Alex Rivera | Logout

Bash script: specify bc output number format

Asked 2010-06-03T11:20:41.843
14

I uses bс to make some calculations in my script. For example:

bc
scale=6
1/2
.500000

For further usage in my script I need "0.500000" instead of ".500000".

Could you help me please to configure bc output number format for my case?

Edit
Report

1 Answer

1

Can you put the bc usage into a little better context? What are you using the results of bc for?

Given the following in a file called some_math.bc

scale=6
output=1/2
print output

on the command line I can do the following to add a zero:

$ bc -q some_math.bc | awk '{printf "%08f\n", $0}'
0.500000

If I only needed the output string to have a zero for formatting purposes, I'd use awk.

answered 2010-06-03T11:42:53.947

Your Answer