Permalink
Browse files

Merge branch 'hrmrebecca'

  • Loading branch information...
2 parents ddbe8bf + b0cce9d commit 33b838471417e5bf1cc87b65a901a17eaa16610e @adamhunter adamhunter committed Dec 2, 2013
Showing with 9 additions and 3 deletions.
  1. +1 −1 lib/dossier/formatter.rb
  2. +8 −2 spec/dossier/formatter_spec.rb
View
2 lib/dossier/formatter.rb
@@ -14,7 +14,7 @@ def number_to_dollars(value)
def commafy_number(value, precision = nil)
whole, fraction = value.to_s.split('.')
- fraction = "%.#{precision}d" % ("0.#{fraction}".to_f.round(precision) * 10**precision).to_i if precision
+ fraction = "%.#{precision}d" % (BigDecimal.new("0.#{fraction}").round(precision) * 10**precision).to_i if precision
[whole.to_i.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,"), fraction].compact.join('.')
end
View
10 spec/dossier/formatter_spec.rb
@@ -38,7 +38,7 @@
describe "custom formatters" do
describe "commafy_number" do
- formats = {
+ {
10_000 => '10,000',
10_000.01 => '10,000.01',
1_000_000_000.001 => '1,000,000,000.001',
@@ -48,17 +48,23 @@
expect(formatter.commafy_number(base)).to eq formatted
end
}
+
it "will return the expected precision if too large" do
expect(formatter.commafy_number(1_000.23523563, 2)).to eq '1,000.24'
end
it "will return the expected precision if too small" do
expect(formatter.commafy_number(1_000, 5)).to eq '1,000.00000'
end
+
+ # h/t to @rodneyturnham for finding this edge case and providing the solution
+ it "will properly format a number given to it" do
+ expect(formatter.commafy_number(1342.58, 2)).to eq '1,342.58'
+ end
end
describe "number_to_dollars" do
- formats = {
+ {
10_000 => '$10,000.00',
10_000.00 => '$10,000.00',
1_000_000_000.000 => '$1,000,000,000.00',

0 comments on commit 33b8384

Please sign in to comment.