Issue #9540 has been updated by Shugo Maeda.

Status changed from Open to Rejected

Samu Voutilainen wrote:
> When I’m trying to floor number with line “"%.2f" % 4.225”, I get 4.22 as result, instead of 4.23. Rounding for example with0.025 correctly results to 0.03.

It's because 4.225 doesn't have an exact representation in binary floating point.

    $ ruby -e 'puts "%.16f" % 4.225'                            
    4.2249999999999996

Please use BigDecimal instead of Float if you need an exact decimal value.

    $ ruby -r bigdecimal -e 'puts BigDecimal("4.225").round(2).to_s("F")'
    4.23


----------------------------------------
Bug #9540: Number formatting with 4.225 fails
https://bugs.ruby-lang.org/issues/9540#change-45331

* Author: Samu Voutilainen
* Status: Rejected
* Priority: Normal
* Assignee: 
* Category: 
* Target version: 
* ruby -v: ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
* Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
Hi,

When I’m trying to floor number with line “"%.2f" % 4.225”, I get 4.22 as result, instead of 4.23. Rounding for example with0.025 correctly results to 0.03.

I tested this with both newest 2.0.0 patchset build and 2.1.0. 

Some example output done with irb:
<pre>
irb(main):016:0> "%.2f" % 4.2251
=> "4.23"
irb(main):017:0> "%.2f" % 4.2250
=> "4.22"
irb(main):018:0> "%.2f" % 4.225
=> "4.22"

irb(main):011:0> "%.2f" % 0.035
=> "0.04"
irb(main):012:0> "%.2f" % 0.025
=> "0.03"
</pre>



-- 
http://bugs.ruby-lang.org/