How can I simplify this piece of code?
if number < 100
divisor = 10
elsif number < 1000
divisor = 100
elsif number < 1000000 # 1 million
divisor = 1000
elsif number < 1000000000 # 1 billion
divisor = 1000000
elsif number < 1000000000000 # 1 trillion
divisor = 1000000000
# Keep goin...
end
The point here is...
- If the number is
>= 0
and< 100
I need adivisor = 10
. - If the number is
>= 100
and< 1.000
I need adivisor = 100
. - If the number is
>= 1.000
and< 1.000.000
I need adivisor = 1.000
. - If the number is
>= 1.000.000
and< 1.000.000.000
I need adivisor = 1.000.000
. - This keeps goin forever..
I tried a few combinations of while
and pow
, but I don't get the pattern here.