Skip to content

Commit e965d26

Browse files
committed
Fix printing of zero BigDecimals
1 parent aaf9f65 commit e965d26

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

lib/truffle/bigdecimal.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,13 @@ def frac
207207
end
208208

209209
def to_s(format = 'E')
210-
if finite?
210+
if zero?
211+
if sign == SIGN_NEGATIVE_ZERO
212+
'-0.0'
213+
else
214+
'0.0'
215+
end
216+
elsif finite?
211217
float_format = format[-1] == 'F' || format[-1] == 'f'
212218
space_frequency = format.to_i
213219
prefix = if self > 0 && [' ', '+'].include?(format[0])

spec/ruby/library/bigdecimal/inspect_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
@bigdec.inspect.should == "0.12345678e4"
1616
end
1717

18+
it "does not add an exponent for zero values" do
19+
BigDecimal("0").inspect.should == "0.0"
20+
BigDecimal("+0").inspect.should == "0.0"
21+
BigDecimal("-0").inspect.should == "-0.0"
22+
end
23+
1824
it "properly cases non-finite values" do
1925
BigDecimal("NaN").inspect.should == "NaN"
2026
BigDecimal("Infinity").inspect.should == "Infinity"

spec/ruby/library/bigdecimal/to_s_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
@bigdec.to_s.should =~ /^0\.[0-9]*e[0-9]*$/
2020
end
2121

22+
it "does not add an exponent for zero values" do
23+
BigDecimal("0").to_s.should == "0.0"
24+
BigDecimal("+0").to_s.should == "0.0"
25+
BigDecimal("-0").to_s.should == "-0.0"
26+
end
27+
2228
it "takes an optional argument" do
2329
lambda {@bigdec.to_s("F")}.should_not raise_error()
2430
end

0 commit comments

Comments
 (0)