Translate

Archives

Image of Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
Image of RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)
Image of Modern Operating Systems (3rd Edition)
Image of Operating System Concepts

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.