I am working on a project which includes using numbers with decimals. The small script below seems to work okay but I'm interested in any comments on this. Some Linux websites came up with other resolves that don't seem to work with the Bash included in Puppy.
1) Is there anything wrong with what I'm using below?
2) Are there easier ways or more proper ways to get the answer?
3) Would the script below fail under other mathematical circumstances (for example, multiplying, longer decimals, etc.)
#!/bin/bash
discount1=".0025"
discount2=".0050"
answer1=`echo "$discount1 + $discount2"|bc`
echo $answer1
Thanks for your comments.
Slavvo67
Bash Decimal Calculations in Scripts
Hi, slavvo67.
No, nothing wrong with your template, I just tried it.
bc will do all operations with decimals. expr will not. I have some experience
calculating with awk, and awk is pretty good at this, even if its syntax appears
sometimes counter-intuitive.
A starting point on awk and calculations could be :
-- https://ixquick.com/do/search?q=calculations+with+awk --
BFN.
musher0
No, nothing wrong with your template, I just tried it.
bc will do all operations with decimals. expr will not. I have some experience
calculating with awk, and awk is pretty good at this, even if its syntax appears
sometimes counter-intuitive.
A starting point on awk and calculations could be :
-- https://ixquick.com/do/search?q=calculations+with+awk --
BFN.
musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
bc -l
Sometimes , I don't know why , but suspect a patch by distributions ,
bc needs the -l option :
-l, --mathlib
Define the standard math library.
Code that may work on one distro without the -l option; may need
the -l option in another distro .
bc needs the -l option :
-l, --mathlib
Define the standard math library.
Code: Select all
answer1=`echo "$discount1 + $discount2"|bc -l`
the -l option in another distro .
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal :P
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal :P