Skip to content

Fix floating formatting#59

Merged
sethdeckard merged 5 commits intosethdeckard:masterfrom
marocchino:fix/58
Mar 12, 2026
Merged

Fix floating formatting#59
sethdeckard merged 5 commits intosethdeckard:masterfrom
marocchino:fix/58

Conversation

@marocchino
Copy link
Copy Markdown
Contributor

close #58

when Float
BigDecimal(number).to_s('F')
else
number.to_s
Copy link
Copy Markdown
Contributor Author

@marocchino marocchino Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> BigDecimal(10).to_s('F')
=> "10.0"

However, the original test expects the result to be 10, so I added a conditional branch to handle this.

def float_format(number)
case duration
when Float
BigDecimal(number).to_s('F')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ cat bench.rb
# frozen_string_literal: true

require 'benchmark/ips'
require 'bigdecimal'

def format_sub(num)
  format('%f', num).sub(/\.?0+$/, '')
end

def big_decimal(num)
  BigDecimal(num).to_s('F')
end

Benchmark.ips do |x|
  x.report('format_sub') { format_sub(1.23456789) }
  x.report('big_decimal') { big_decimal(1.23456789) }
  x.compare!
end
$ ruby bench.rb
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
Warming up --------------------------------------
          format_sub   126.984k i/100ms
         big_decimal   164.232k i/100ms
Calculating -------------------------------------
          format_sub      1.273M (± 0.7%) i/s  (785.70 ns/i) -      6.476M in   5.088579s
         big_decimal      1.638M (± 0.7%) i/s  (610.61 ns/i) -      8.212M in   5.014346s

Comparison:
         big_decimal:  1637706.2 i/s
          format_sub:  1272746.5 i/s - 1.29x  slower

@sethdeckard
Copy link
Copy Markdown
Owner

Hi @marocchino thank you for the PR, would you please address the following?

  • Fix spec.dependencies (should be add_dependency)
  • Move float_format to AttributeFormatter (looks like we'll need it for other types that have this same issue and I thought this was better than making it private).

The same issue exists in PartItem and DateRangeItem via unquoted_format but I can handle this if you just want to keep this PR focused, up to you.

@marocchino marocchino force-pushed the fix/58 branch 2 times, most recently from fce02cd to 1c02215 Compare March 12, 2026 08:12
@marocchino marocchino force-pushed the fix/58 branch 2 times, most recently from 4a96d39 to 081031b Compare March 12, 2026 08:21
Copy link
Copy Markdown
Owner

@sethdeckard sethdeckard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@sethdeckard sethdeckard merged commit a9c7c44 into sethdeckard:master Mar 12, 2026
4 checks passed
@marocchino marocchino deleted the fix/58 branch March 12, 2026 19:01
sethdeckard added a commit that referenced this pull request Mar 12, 2026
Update changelog crediting marocchino for fixing
floating-point formatting issue (#58, PR #59).
sethdeckard added a commit that referenced this pull request Mar 12, 2026
Update changelog crediting marocchino for fixing
floating-point formatting issue (#58, PR #59).
@sethdeckard
Copy link
Copy Markdown
Owner

@marocchino released in 1.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: Incorrect Duration Formatting (Scientific Notation)

2 participants