fardin - 1 year ago 40

Ruby Question

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
```

Source (Stackoverflow)