# Multiply with decimal numbers in bash [closed]

When i run

``````p=0.1
k=2
while [ \$p \< 1 ]
do
echo \$p
p=\$(echo "scale=2; \$p*\$k" | bc)
done
``````

i have this result

``````0.1
.2
.4
.8
``````

What can i do to have 0.1, 0.2, 0.4 and 0.8 ?

-

## closed as off topic by Rinzwind, hhlp, LnxSlck, Jorge Castro, TachyonsNov 20 '12 at 18:47

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Before the echo check with an if if the 1st char of p is a point and if so add a 0 in front of it? – Rinzwind Nov 20 '12 at 15:36
this is a pure programming question and probably belongs on stackoverflow instead... – ImaginaryRobots Nov 20 '12 at 16:17

Use `printf` instead of echo to format it:

`printf %0.1f\\n \$p`

Output:

``````\$ p=0.1; k=2
\$ while [ \$p \< 1 ]; do     printf %0.1f\\n \$p ;     p=\$(echo "scale=2; \$p*\$k" | bc); done
0.1
0.2
0.4
0.8
``````
-

I would do use seq and printf to solve my problem. check this:

``````for i in \$(seq .1 .2 1); do printf "%0.1f\n" "\$i"; done
``````