Bash Multiplying Decimal to int
Asked Answered
F

5

15

I read price from user input. When i multiply the input with int like this

T="$((PRICE*QTY))"|bc; gives line 272: 12.00: syntax error: invalid arithmetic operator (error token is ".00") or .50

depending on user input. How do i multiply these two variables and get a total with 2 decimal points?

Flask answered 19/7, 2010 at 9:41 Comment(2)
WOW! This is good! I've never seen replies this quick in any other forums no matter how active they are and mostly get "we wont do your assignment for you" replies. This worked for me T=echo $QTYS\* $PRICE | bc; Its also rounding price to 2 decimal places without scale. For some reason just price*QTY was giving me command not found error.Flask
One thing to remember is that bash doesn't truly have any data types except strings. Every variable comes down to a string, so it may help you to remember that. Different programs of course will treat certain strings in a special way.Highboy
C
23

this works:


PRICE=1.1
QTY=21
RES=$(echo "scale=4; $PRICE*$QTY" | bc)
echo $RES
Clotilda answered 19/7, 2010 at 9:55 Comment(2)
Could you also explain the use of scale here? What does scale=4 mean.Iila
This is what I was missing when using bc! scale sets the precision of the operation - see the "Variables" section of this pageNever
E
13
var=$(echo "scale=2;$PRICE*$QTY" |bc)

You can also use awk

awk -vp=$PRICE -vq=$QTY 'BEGIN{printf "%.2f" ,p * q}'
Eelpout answered 19/7, 2010 at 9:48 Comment(5)
./menu3.sh: line 278: 12.25: syntax error: invalid arithmetic operator (error token is ".25")Flask
remove the $(()) . bash doesn't do floating arithmetic. If you want to set 2 decimal places, use scale=2Eelpout
You need dollar signs before each variable name.Lubric
var=$(echo "scale=2;$PRICE*$QTY" |bc) gives me (standard_in) 2: syntax errorFlask
bc is not installed on all distributions. I upvoted this one because awk is and he supplied an awk version that is more portable to *nix systems!Interferometer
E
4
T="$(echo "$PRICE*$QTY" | bc)"
Egoism answered 19/7, 2010 at 9:45 Comment(2)
i get (standard_in) 2: syntax error idk why. its almost the same as aboveFlask
@svenus: This one works for me. I don't know why you're getting that error from bc.Lubric
F
2

You can use mul=0.8 exp=200 texp=awk -vp=$mul -vq=$exp 'BEGIN{printf "%.2f" ,p * q}'

Hope this is going to work.

Flapjack answered 27/4, 2018 at 4:57 Comment(1)
Please edit your answer. Its right, but not properly formattedModestamodeste
G
1

First, trying to do floating-point arithmetic with bc(1) without using the -l flag is bound to give you some funny answers:

sarnold@haig:~$ bc -q
3.5 * 3.5
12.2
sarnold@haig:~$ bc -q -l
3.5 * 3.5
12.25

Second, the $((...)) is an attempt to do arithmetic in your shell; neither my bash nor dash can handle floating point numbers.

If you want to do the arithmetic in your shell, note printf(1) as well as (probably) your shell's built-in printf function. If you want to do the arithmetic in bc, note the special variable scale.

Garek answered 19/7, 2010 at 9:58 Comment(1)
most of the $(( )) with bc i tried gave me errors. Must be because of the (( ))? Thanks for the clarification.Flask

© 2022 - 2024 — McMap. All rights reserved.