How do I print the "zero" between the minus and the decimal places on Bash shell

295 Views Asked by At

With dc external tool bash tend to not print at the right of the decimal point.

#!/bin/bash    
a[0]=-0.5    
`echo "scale=10;${a[0]}/1"|bc -l`

With the command represented above bash will print -.5000000000.

How can I add the zero between the minus signal and the point -0.5000000000

PS: I do print a[1]=0 with 10 decimal cases?

1

There are 1 best solutions below

2
anubhava On

Instead of bc you may consider this awk that does floating point match and formatting using printf:

a[0]=-0.5

awk -v n="${a[0]}" 'BEGIN{printf "%.10f\n", n/1}'

-0.5000000000