Home > Software design >  Not able to get float value as an output in shell scripting
Not able to get float value as an output in shell scripting

Time:07-06

When I try to divide values like you can see in below code snippet when value gets in float it doesn't shows me the float? It just prints 0. I'm doing MB to GB conversion. Anybody can help me to let me see the conversion to some 2-3 values after 0. like 0.2545

Taking an input from user:

echo -e "Enter your value in MB! \c"
read -r userINPUT

Converted result:

echo "In GB it is: $((userINPUT / 1024))"

Output I'm getting:

Enter your Frist Number! 25
In GB it is: 0

CodePudding user response:

Bash does not support floating point arithmetic.

You can use bc command to do the operations on float values.

Like:

bc <<< "scale=2;  $userINPUT / 1024 "

Or better you can install and use the command numfmt for the conversion

https://man7.org/linux/man-pages/man1/numfmt.1.html

  • Related