- 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 + 2 comments 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?

mehtakajal03 + 0 comments what does -l do in this command?

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 + 2 comments 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 + 1 comment 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...

admin70 + 0 comments It was in the "PROBLEM" : **Your task is to evaluate the expression and display the correct output rounding upto decimal places. **

admin70 + 0 comments Why did this comment get -31?

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.

Sort 106 Discussions, By:

Please Login in order to post a comment