Translate

Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)
Image of Android Wireless Application Development
Image of Operating System Concepts
Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)

Convert Integer to Float in Korn Shell

There is no explicit mechanism, such as float(integer), in the Korn Shell (ksh93) to convert an integer to a float which in the Korn Shell is by default a long double.

By default, dividing integer 1 by 3 produces 0:

$ integer x=1
$ echo $(( x/3 ))
0
$


So how to you get the result, 0.3333 …, which you probably were expecting?

You can convert x to floating point and then do the division by replacing the 3 with either 3. or, as I normally do, with 3.0:

$ integer x=1
$ echo $(( x/3. ))
0.333333333333333333
$


Note the Bash shell does not support floating point so this will not work with Bash.

Comments are closed.