Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10099 (closed defect: fixed)

Opened 1 year ago

Last modified 8 months ago

[PATCH] number_to_human_size remove zeros at the end

Reported by: libc Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: normal Keywords: verified
Cc:

Description

Symptoms:

>> helper.number_to_human_size(70)
=> "7 Bytes"
>> helper.number_to_human_size(700)
=> "7 Bytes"

Patch:

  • test/template/number_helper_test.rb

    old new  
    8787    assert_equal '1.01 KB',   number_to_human_size(1.0100.kilobytes, 4) 
    8888    assert_equal '10 KB',   number_to_human_size(10.000.kilobytes, 4) 
    8989    assert_equal '1 Byte',   number_to_human_size(1.1) 
     90    assert_equal '10 Bytes', number_to_human_size(10) 
    9091    assert_nil number_to_human_size('x') 
    9192    assert_nil number_to_human_size(nil) 
    9293  end 
  • lib/action_view/helpers/number_helper.rb

    old new  
    170170          when size < 1.gigabyte; "%.#{precision}f MB"  % (size / 1.0.megabyte) 
    171171          when size < 1.terabyte; "%.#{precision}f GB"  % (size / 1.0.gigabyte) 
    172172          else                    "%.#{precision}f TB"  % (size / 1.0.terabyte) 
    173         end.sub(/([0-9])\.?0+ /, '\1 ' ) 
     173        end.sub(/((?:[0-9]\.[0-9]*[1-9])|(?:[0-9]+(?=\.)))\.?0+ /, '\1 ' ) 
    174174      rescue 
    175175        nil 
    176176      end 

Attachments

actionpack.patch (1.3 kB) - added by libc on 11/08/07 01:51:43.
patch (from vendor/rails r8112)
simpler_actionpack.patch (1.3 kB) - added by developingchris on 11/08/07 03:50:46.
a simpler version of the previous path

Change History

11/08/07 01:51:43 changed by libc

  • attachment actionpack.patch added.

patch (from vendor/rails r8112)

11/08/07 03:35:54 changed by developingchris

Couldn't this be simpler, the whole purpose is to delete trailing zeros on a decimal. So that regex line could be end.sub(/(([0-9])\.0+ /, '\1 ' )

Its the optional period which makes the original line fail since it only matches the first character next to the period so for 800.00 GB "0.00 " is matched and replaced with 0 but if the optional is left in 800 GB still matches "800 " and the zeros are deleted.

11/08/07 03:50:46 changed by developingchris

  • attachment simpler_actionpack.patch added.

a simpler version of the previous path

11/08/07 14:11:05 changed by developingchris

+1, cleaned it up a little, passes same tests

01/15/08 17:02:56 changed by bregor

+1

01/15/08 17:04:48 changed by vzctl

+1

01/15/08 17:10:46 changed by libc

  • keywords set to verified.

01/15/08 17:20:32 changed by kronos

+1

02/02/08 02:59:13 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [8774]) Fix number_to_human_size incorrectly removing trailing zeros. Closes #10099 [libc, developingchris]

02/02/08 03:00:28 changed by bitsweat

(In [8775]) Merge r8774 from trunk: fix number_to_human_size incorrectly removing trailing zeros. References #10099.