mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make number_to_human and number_with_precision work with negatives
This commit is contained in:
parent
d717ff9e52
commit
e20012b64b
2 changed files with 4 additions and 2 deletions
|
@ -260,7 +260,7 @@ module ActionView
|
|||
if number == 0
|
||||
digits, rounded_number = 1, 0
|
||||
else
|
||||
digits = (Math.log10(number) + 1).floor
|
||||
digits = (Math.log10(number.abs) + 1).floor
|
||||
rounded_number = BigDecimal.new((number / 10 ** (digits - precision)).to_s).round.to_f * 10 ** (digits - precision)
|
||||
end
|
||||
precision = precision - digits
|
||||
|
@ -459,7 +459,7 @@ module ActionView
|
|||
raise ArgumentError, ":units must be a Hash or String translation scope."
|
||||
end.keys.map{|e_name| DECIMAL_UNITS.invert[e_name] }.sort_by{|e| -e}
|
||||
|
||||
number_exponent = number != 0 ? Math.log10(number).floor : 0
|
||||
number_exponent = number != 0 ? Math.log10(number.abs).floor : 0
|
||||
display_exponent = unit_exponents.find{|e| number_exponent >= e }
|
||||
number /= 10 ** display_exponent
|
||||
|
||||
|
|
|
@ -83,6 +83,7 @@ class NumberHelperTest < ActionView::TestCase
|
|||
end
|
||||
|
||||
def test_number_with_precision
|
||||
assert_equal("-111.235", number_with_precision(-111.2346))
|
||||
assert_equal("111.235", number_with_precision(111.2346))
|
||||
assert_equal("31.83", number_with_precision(31.825, :precision => 2))
|
||||
assert_equal("111.23", number_with_precision(111.2346, :precision => 2))
|
||||
|
@ -184,6 +185,7 @@ class NumberHelperTest < ActionView::TestCase
|
|||
end
|
||||
|
||||
def test_number_to_human
|
||||
assert_equal '-123', number_to_human(-123)
|
||||
assert_equal '0', number_to_human(0)
|
||||
assert_equal '123', number_to_human(123)
|
||||
assert_equal '1.23 Thousand', number_to_human(1234)
|
||||
|
|
Loading…
Reference in a new issue