Ruby Numeric. Help - Sorry for bothering. RESOLVED
-
I have to display some values, and most of all are integers, but some will be float. This is why I must use float.
I want to get rid of zero to that values that don't require it.
This is what I want:%(#4000BF)[3.3 => 3.3
4.8 => 4.8]
16.0 => 16I could do the trick in many ways but I'm still looking for something native.
There is no native way, but I find this on web:
#extend the Float class class Float def to_i_if if self - floor == 0 floor else self end end endBut here will be a problem: if I have a number like this: 4.00001, won't be converted to integer. For that, the method to_i_if can be enhanced:
class Float def to_i_if(threshold=0) if self - floor <= threshold floor else self end end endtreshold => display format like this: 0.01 (how many zeroes you need)
I found this on web and I thought to share it.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement