- Practice
- Linux Shell
- Bash
- Arithmetic Operations
- Discussions

# Arithmetic Operations

# Arithmetic Operations

varunyellina + 5 comments When you use

``echo "scale=3; `$sum/$`x | bc"``

your expected result would be with

`3`

decimal points. You should note that there is there is no**rounding off**done by**bc**.

Now in bash you can use the

**printf**function to do the rounding off that you require. Use`scale = 4`

and then use**printf**to round off to 3 decimal points using`"%.3f"`

ChienliMa + 0 comments then why not only using printf?

abhishektyagi + 5 comments `printf "%.3f"`

`e"|bc)`

gentunian + 2 comments just avoid the

`scale`

.`printf "%.3f"`

will be sufficientbewuethr + 1 comment Avoiding

`scale`

defaults to`scale=0`

, so that won't work.kamalraghav + 1 comment then whats the use of setting precision in printf

maciektr + 0 comments It rounds the number got from bc to 3 decimal points. Bc does no rounding.

deikensentsu + 0 comments As @bewuethr mentions avoiding scale won't work, but if you used bc -l it would as this defaults to scale of 20.

ashwinjkm713 + 0 comments thanks :)

hardik_mehta + 0 comments printf "%.3f\n" exp | bc -l)

ken_cristian + 1 comment There is a $ before the parenthesis that is being interpreted in the comment

printf "%.3f" $(echo $exp | bc -l)

sjames2 + 0 comments Why do you need the "$" before the echo part?

stillone + 0 comments above command not able to write in STDOUT

asprabhune + 0 comments read num echo $num | bc -l | xargs printf "%.*f\n" 3

michellerodri247 + 0 comments The basic arithmetic operations are very important for solving any mathematical problem build your own engagement ring online. It is the basics of Mathematics. Addition, subtraction, multiplication, and division is a basic arithmetic operation. Also, a more advanced operation is there. it helps to handle many of the difficult problems.

sreya92 + 1 comment Pretty straightforward way to do it, feel free to correct if it's wrong or can be improved:

read num echo $num | bc -l | xargs printf "%.3f"

15PA5A0505 + 1 comment what is -l?

markhkamp + 0 comments From "man bc":

If bc is invoked with the -l option, a math library is preloaded and the default scale is set to 20.

In this case, the important part is setting the scale.

Dillanm + 2 comments Having the same issues as everyone else here;

`scale = 3`

gives 17.928 NOT 17.929. Although I know that the answer should be 17.929, quite an odd problem.EDIT:

You need to use the

`printf`

method for it to work, the reason is that`-bc`

literally chops off the end of the number instead of actually rounding.PRASHANTB1984Challenge Author + 1 comment the actual answer is 17.9285 - which needs to be (upgraded) to 17.929 the reason why most people faltered is because they truncated rather than rounding off.

pygospa + 0 comments Would have been nice if there was an info on that in the problem description... bc -l is described in great detail and used but nowhere is it pointed out that it truncates, or that the problem would need proper roundings... pretty frustrating, as arithmetical rounding is hardly ever requested in CS problems and everybody is fine with truncating...

asprabhune + 0 comments read num echo $num | bc -l | xargs printf "%.*f\n" 3

pieta_absurdum + 0 comments http://stackoverflow.com/questions/26465496/rounding-up-float-point-numbers-bash

I found this on stackoverflow...Even people there are using printf or even piping it to python script???I thought i would learn something here..If we need to learn how to round up than make an example or put a good link on the site...and not the links that are here for examples..

Giridhar_L + 3 comments This works

read word value=` echo "$word " | bc -l ` echo $value | python -c "print round(float(raw_input()),3)"

saisubramanian97 + 1 comment Bro!!!! U r genius man!!

findkpavan + 0 comments calling python interpreter for a simple rounding is an overkill. printf will be enough to do this.

mustafanadeem627 + 0 comments What is -L doing here? and -c?

FRG96 + 0 comments This, my dear folks, is called a workaround.

hrandhawa1993 + 1 comment This works

read X printf "%.3f" $(echo "scale=4;$X" | bc)

seb_fisher + 0 comments or you could use bc -l instead of scale=4

Phr34Ck + 5 comments Here's the result from a calculator: 5+50*3/20 + (19*2)/7 = 17.92857142857143

and yet you're expecting the solution to be: 17.929 ....

The solution is literally 1 line and I cannot see why you're expecting 17.929 instead of 17.928

PRASHANTB1984Challenge Author + 1 comment hint: use bc -l. Use 'scale' or an equivalent for appropriate decimal places! As you can see, many have got this challenge right. 17.92857 rounds off to 17.949 when corrected to 3 decimal places.

tremblerz + 1 comment I have used bc -l along with scale , still it rounds off it to 17.92857

thomasvictorio + 0 comments Use a bc scale that's higher than printf's. e.g. Use scale=4 together with printf "%.3f". Only then you'll get hackerrank's expected result.

Phr34Ck + 1 comment Please take a look at my submission:

read X echo "scale = 3; $X" | bc -l

this is not passing the first test for that specific error. All other tests are passing fine.

M_F_H + 0 comments you are missing a " ; " after read X ... also,

`bc`

truncates in a strange way and the challenge author, in spite of writing (now) "...rounded**up**...", expects a value rounded in another strange way. (Seems there are solutions using printf, but for doing it within bc only I had to use "... if ( x < 0 ) { x = x - 0.0004999;} else { x = x + 0.0004999; } ..." which looks highly nonstandard to me. (For normal mathematicians, round(x) := floor(x+0.5), so round(1.5) = 2 and round(-1.5) = -1. This is not the convention used here...)

PRASHANTB1984Challenge Author + 1 comment it's a one line answer - can't really give a bigger hint without revealing the answer altogether :)

danthemango + 0 comments Instead of saying "display the output correct to 3 decimal places" you should say "display the output

**rounded**to 3 decimal places".'Correct' just means that users should give you what you want, and you never say that you want it rounded and not truncated.

cielavenir + 1 comment I had the same problem and I used printf approach, which should not be what is expected...

grvtyagi22 + 0 comments printf "%.3f" $(echo "scope=3;5+50*3/20 + (19*2)/7"|bc -l)

PRASHANTB1984Challenge Author + 1 comment No, leafmoon - people have got it correct. Or, message me the code you submitted and I'll try to see what the issue is.

gentunian + 0 comments using

`scale`

won't round the number. You could just try it yourself.

EBhoraniya + 1 comment echo "scale = 3; $a" | bc, gives 17.928 for case-0,while other cases gives right answer. What should i do to make it correct for all cases?

ShubhamDingar + 0 comments read X;

printf "%.3f" X"|bc -l)

Big_Leg_Davis + 1 comment http://stackoverflow.com/questions/26861118/rounding-numbers-with-bc-in-bash check this one out people, should give you the answers you're asking for

marino + 0 comments Cool, It just implement a

`round`

functions, without uses`printf`

abbshr + 1 comment bc <<< "scale = 4; $expression" | xargs printf "%.3f"

rajkiran0gade + 0 comments This approach is good. Thank you.

Sort 98 Discussions, By:

Please Login in order to post a comment