Skip to content

Commit

Permalink
Merge branch 'hrmrebecca'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhunter committed Dec 2, 2013
2 parents ddbe8bf + b0cce9d commit 33b8384
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/dossier/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions spec/dossier/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 33b8384

Please sign in to comment.