Hi, Folks,
I''ve submited a patch(http://dev.rubyonrails.org/ticket/11409) to fix
bug in ticket #8027 and #8275, they are both caused by Ruby''s sprintf
bug:
irb> "%01.2f" % "31.825"
=> "31.82"
irb> "%01.2f" % "32.825"
=> "32.83"
#8275 provides a solution to initial number with BigDecimal, but it
only work for Mac,
My solution is round the number before pass them to sprintf, and I''ve
tested that it works for both Mac, Windows and Linux:
def number_with_precision(number, precision=3)
"%01.#{precision}f" % ((Float(number) * (10 **
precision)).round.to_f / 10 ** precision)
end
irb> number_with_precision(31.825, 2)
=> "31.83"
irb> number_with_precision(32.825, 2)
=> "32.83"
Your feedback will be appreciate, becuase my project heavily depend on
number_with_precision.
Thanks,
Yuanyi
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---