Is there any worthy Ruby method to count the number of digits in a float? Also, how do I specify the precise when to_s float numbers?
A:
I think you should check out the number_with_precision helper.
number_with_precision(13, :precision => 5) # => 13.00000
Mike Buckbee
2009-10-02 20:20:23
I'm looking for the Ruby solution, not Rails
gmile
2009-10-02 20:38:45
Sorry, missed that.
Mike Buckbee
2009-10-02 21:37:44
+3
A:
# 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
khelll
2009-10-02 20:51:56