Skip to content

Commit

Permalink
Fix BigDecimal.to_s (#9897)
Browse files Browse the repository at this point in the history
  • Loading branch information
dukenguyenxyz authored Nov 11, 2020
1 parent 11e1053 commit 3a1f06d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 2 additions & 0 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ describe BigDecimal do
BigDecimal.new(0).to_s.should eq "0"
BigDecimal.new(1).to_s.should eq "1"
BigDecimal.new(-1).to_s.should eq "-1"
BigDecimal.new("-0.35").to_s.should eq "-0.35"
BigDecimal.new("-.35").to_s.should eq "-0.35"
BigDecimal.new("0.01").to_s.should eq "0.01"
BigDecimal.new("-0.01").to_s.should eq "-0.01"
BigDecimal.new("0.00123").to_s.should eq "0.00123"
Expand Down
3 changes: 2 additions & 1 deletion src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ struct BigDecimal < Number
io << '0'
end
io << s[1..-1]
elsif (offset = s.size - @scale) == 1 && @value < 0
io << "-0." << s[offset..-1]
else
offset = s.size - @scale
io << s[0...offset] << '.' << s[offset..-1]
end
end
Expand Down

0 comments on commit 3a1f06d

Please sign in to comment.