Skip to content

Commit 3d49d0a

Browse files
floriandrew
authored andcommitted
Avoid crashing on Ruby 2.4 for numeric strings (#470)
1 parent 4135da1 commit 3d49d0a

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/split/dashboard/helpers.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def number_to_percentage(number, precision = 2)
1818
end
1919

2020
def round(number, precision = 2)
21-
BigDecimal.new(number.to_s).round(precision).to_f
21+
begin
22+
BigDecimal.new(number.to_s)
23+
rescue ArgumentError
24+
BigDecimal.new(0)
25+
end.round(precision).to_f
2226
end
2327

2428
def confidence_level(z_score)

spec/dashboard_helpers_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,19 @@
2424
expect(confidence_level(2.58)).to eq('99% confidence')
2525
expect(confidence_level(3.00)).to eq('99% confidence')
2626
end
27+
28+
describe '#round' do
29+
it 'can round number strings' do
30+
expect(round('3.1415')).to eq BigDecimal.new('3.14')
31+
end
32+
33+
it 'can round number strings for precsion' do
34+
expect(round('3.1415', 1)).to eq BigDecimal.new('3.1')
35+
end
36+
37+
it 'can handle invalid number strings' do
38+
expect(round('N/A')).to be_zero
39+
end
40+
end
2741
end
2842
end

0 commit comments

Comments
 (0)