1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits (closes #6231) [phallstrom]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5249 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2006-10-09 00:59:19 +00:00
parent f9650a23f0
commit a49e7d5c0c
3 changed files with 20 additions and 3 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Fixed that NumberHelper#number_to_delimiter should respect precision of higher than two digits #6231 [phallstrom]
* Fixed that FormHelper#radio_button didn't respect an :id being passed in #6266 [evansj]
* Added an html_options hash parameter to javascript_tag() and update_page_tag() helpers #6311 [tzaharia]. Example:

View file

@ -71,10 +71,16 @@ module ActionView
# Formats a +number+ with a +delimiter+.
# Example:
# number_with_delimiter(12345678) => 12,345,678
def number_with_delimiter(number, delimiter=",")
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
def number_with_delimiter(number, delimiter=",", separator=".")
begin
parts = number.to_s.split(separator)
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
parts.join separator
rescue
number
end
end
# Returns a formatted-for-humans file size.
#
# Examples:

View file

@ -31,6 +31,15 @@ class NumberHelperTest < Test::Unit::TestCase
def test_number_with_delimiter
assert_equal("12,345,678", number_with_delimiter(12345678))
assert_equal(nil, number_with_delimiter(nil))
assert_equal("0", number_with_delimiter(0))
assert_equal("123", number_with_delimiter(123))
assert_equal("123,456", number_with_delimiter(123456))
assert_equal("123,456.78", number_with_delimiter(123456.78))
assert_equal("123,456.789", number_with_delimiter(123456.789))
assert_equal("123,456.78901", number_with_delimiter(123456.78901))
assert_equal("123,456,789.78901", number_with_delimiter(123456789.78901))
assert_equal("0.78901", number_with_delimiter(0.78901))
end
def test_number_to_human_size