fardin fardin - 5 months ago 10
Ruby Question

How to turn float decimal values into integers in ruby

How can one get the decimal values of a float and turn it into an integer in ruby?

Here is how far I got so far:

number = 12.55
decimal_length = number.to_s.split('.')[1].size #=> 2
decimal = number.divmod(1)[1].round(decimal_length) #=> 0.55


Here how can I turn decimal into integer (55) in a way that it would work with any number as input?

Answer

Maybe I'm misunderstanding? But you've got it you're just doing extra work.

number = 12.55
number.to_s.split('.')[1].to_i
#  => 55