0

I am trying to find square root of 12345 and then scale it to 100 decimal places using the command shown below:

val=$(bc <<< "scale=100 ; sqrt ( 12345) " )

But the problem is I want to rescale the value to 10 decimal places using bc only in echo command but it is not working, I tried following code of lines but none worked.

echo "scale=10 ; "$val" " | bc

echo "scale=10 ; $val " | bc

echo "scale=10 ; val " | bc

Can somebody here help me and make out the difference.

Note: I don't want to directly scale the value to 10 places in sqrt() because last place of decimal value differ from the answer so to have high accuracy I have to do in this way.

1
1

If you are not worried about rounding in bc, you can do:

$ echo "scale=10 ; "$val/1" " | bc
111.1080555135

It is when a value gets divided (even by 1) that the number of decimals gets adjusted to the scale.

Of course, you can always use the shell printf (again, some rounding might crawl in).

$ printf '%5.10f\n' "$val"
111.1080555135

Why do you need precision?

1
  • Oh thnx btw printf "%.10f" $(bc -q <<< scale =10 \; $a ) actually worked for me as suugested by first comment in my question , I need precise value for some sharp manipulation. – solver Apr 10 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.