Sort 84 Discussions, By:
Please Login in order to post a comment
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"
scale = 4
then why not only using printf?
just avoid the scale.
printf "%.3f" will be sufficient
Avoiding scale defaults to scale=0, so that won't work.
then whats the use of setting precision in printf
It rounds the number got from bc to 3 decimal points. Bc does no rounding.
As @bewuethr mentions avoiding scale won't work, but if you used bc -l it would as this defaults to scale of 20.
printf "%.3f\n" exp | bc -l)
There is a $ before the parenthesis that is being interpreted in the comment
printf "%.3f" $(echo $exp | bc -l)
Why do you need the "$" before the echo part?
above command not able to write in STDOUT
echo $num | bc -l | xargs printf "%.*f\n" 3
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.
scale = 3
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.
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.
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...
Pretty straightforward way to do it, feel free to correct if it's wrong or can be improved:
echo $num | bc -l | xargs printf "%.3f"
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..
value=` echo "$word " | bc -l `
echo $value | python -c "print round(float(raw_input()),3)"
Bro!!!! U r genius man!!
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
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.
I have used bc -l along with scale , still it rounds off it to 17.92857
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.
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.
it's a one line answer - can't really give a bigger hint without revealing the answer altogether :)
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.
I had the same problem and I used printf approach, which should not be what is expected...
printf "%.3f" $(echo "scope=3;5+50*3/20 + (19*2)/7"|bc -l)
No, leafmoon - people have got it correct. Or, message me the code you submitted and I'll try to see what the issue is.
using scale won't round the number. You could just try it yourself.
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?
printf "%.3f" X"|bc -l)
printf "%.3f" $(echo "scale=4;$X" | bc)
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
Cool, It just implement a round functions, without uses printf
bc <<< "scale = 4; $expression" | xargs printf "%.3f"
This approach is good.