Skip to content

Commit ac2440e

Browse files
committed
[GR-20257] Fix BigDecimal#to_s 5F formatting issue (#1711).
PullRequest: truffleruby/1205
2 parents eca4493 + 06045e0 commit ac2440e

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Bug fixes:
4343
* Fixed BigDecimal coerce initial argument using `to_str` (#1826).
4444
* Make `Enumerable#chunk` work without a block (#1518).
4545
* Fixed issue with `SystemCallError.new` setting a backtrace.
46+
* Fixed `BigDecimal#to_s` formatting issue (#1711).
4647

4748
Compatibility:
4849

lib/truffle/bigdecimal.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ def to_s(format = 'E')
242242

243243
format '%s%s.%s',
244244
prefix,
245-
add_spaces_to_s(before_dot, true, space_frequency),
246-
add_spaces_to_s(after_dot, false, space_frequency)
245+
add_spaces_to_s(before_dot, space_frequency),
246+
add_spaces_to_s(after_dot, space_frequency)
247247
else
248248
format '%s0.%se%d',
249249
prefix,
250-
add_spaces_to_s(unscaled_value, false, space_frequency),
250+
add_spaces_to_s(unscaled_value, space_frequency),
251251
exponent_value
252252
end
253253
else
@@ -270,19 +270,14 @@ def self._load(data)
270270
BigDecimal(data.split(':').last)
271271
end
272272

273-
private def add_spaces_to_s(string, reverse, space_frequency)
273+
private def add_spaces_to_s(string, space_frequency)
274274
return string if space_frequency == 0
275275

276276
remainder = string.size % space_frequency
277-
shift = reverse ? remainder : 0
278-
pieces = (string.size / space_frequency).times.map { |i| string[space_frequency*i + shift, space_frequency] }
277+
pieces = (string.size / space_frequency).times.map { |i| string[space_frequency*i, space_frequency] }
279278

280279
if remainder > 0
281-
if reverse
282-
pieces.unshift string[0...remainder]
283-
else
284-
pieces.push string[-remainder..-1]
285-
end
280+
pieces.push string[-remainder..-1]
286281
end
287282

288283
pieces.join ' '

spec/ruby/library/bigdecimal/to_s_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
str1 = '-123.45678 90123 45678 9'
4343
BigDecimal("-123.45678901234567890").to_s('5F').should == str1
44+
BigDecimal('1000010').to_s('5F').should == "10000 10.0"
4445
# trailing zeroes removed
4546
BigDecimal("1.00000000000").to_s('1F').should == "1.0"
4647
# 0 is treated as no spaces

0 commit comments

Comments
 (0)