user3684042
user3684042

Reputation: 671

Determining the number of decimals in a float number

I want to run a command (such as ls -lrt) 49 times and every time 20 milliseconds after the previous run. What I have written in my bash file is:

 for i in `seq 1 49`;
     do
     v=6.$((i*20)  
     sleep $v && ls -lrt
     done

But it apparently does not differentiate cases like where i equals to 4 with the one that i equals to 40 as both result in v=6.8. What I need is to wait 6.080 for i=4 and 6.800 for i=40.

Upvotes: 2

Views: 33

Answers (2)

Kent
Kent

Reputation: 195209

how about v=$(echo "scale=2;6+$i*0.02"|bc)

this will keep increasing if the result was greater than 7, although it won't happen till 49. But personally I think it is better than string concatenation.

Upvotes: 1

choroba
choroba

Reputation: 241998

You can use printf to format the number:

printf -v v '6.%03d' $((i*20))

-v v specifies that the variable $v should hold the result.

Upvotes: 2

Related Questions