Pregunta

¿Hay algún método de Ruby digno para contar el número de dígitos en un flotador? Además, ¿cómo especifico los números precisos cuando To_s Float?

¿Fue útil?

Solución

# Number of digits

12345.23.to_s.split("").size -1 #=> 7

# The precious part

("." + 12345.23.to_s.split(".")[1]).to_f #=> .023

# I would rather used 
# 12345.23 - 12345.23.to_i 
# but this gives 0.22999999999563

Otros consejos

to specify precision of a float in Ruby. you can use the round method.

number.round(2)

2 is the precision.

53.819.round(2) -> 53.82

I think you should check out the number_with_precision helper.

number_with_precision(13, :precision => 5) # => 13.00000
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top