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:
"%01.2f" % "31.825"
=> "31.82"
"%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
number_with_precision(31.825, 2)
=> "31.83"
number_with_precision(32.825, 2)
=> "32.83"
Your feedback will be appreciate, becuase my project heavily depend on number_with_precision.
Thanks,
Yuanyi