Translate

Image of RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)
Image of Modern Operating Systems (3rd Edition)
Image of Beginning Google Maps API 3
Image of Linux Kernel Development (3rd Edition)

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.