hankyo
hankyo

Reputation: 583

Count how many decimal places in a decimal number in bash shell

I have a decimal number:

num=0.000001

I want to get the decimal places count, using bash shell

Required output:

decimalPointsCount=$(code to get decimal places length of $num variable)

Any awk,sed,perl..etc suggestions would be appreciated,Thanks

Upvotes: 0

Views: 956

Answers (1)

Quasímodo
Quasímodo

Reputation: 4004

The shell alone can do that:

num=0.000001
decimals=${num#*.}              #Removes the integer part and the dot (=000001)
decimalPointsCount=${#decimals} #Counts the length of resulting string (=6)

Upvotes: 1

Related Questions