LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Decimal numbers in bash script variables? (https://www.linuxquestions.org/questions/programming-9/decimal-numbers-in-bash-script-variables-380924/)

Massif 11-07-2005 06:33 PM

Decimal numbers in bash script variables?
 
Is it possible to store fractional numbers in variables in bash scripts? I can only seem to save integers.

If it's not possible, does anyone know how I could do fractional calculations using only integers?

tangle 11-07-2005 06:47 PM

This is a shot in the dark for me, but did you try to put the numbers in quotes? It works for white spaces, not sure about decimal numbers.

Massif 11-07-2005 06:56 PM

Yeah I tried that.

I actually found my answer. Google :D

Bash itself cannot support floating point numbers, but there is a program called bc that can do decimal arithmetic.

here's the basic syntax

answer=$(echo "scale=9; $x *1.423" | bc)

The scale value is the number of decimal spaces you want. I'm still not sure if you can initialize a variable to a decimal number, but I got my stuff working.

chrism01 11-07-2005 09:01 PM

You store anything as a "string", but when you want to calculate, you'll have to use bc ...


All times are GMT -5. The time now is 01:29 AM.