Fix floating formatting#59
Merged
sethdeckard merged 5 commits intosethdeckard:masterfrom Mar 12, 2026
Merged
Conversation
marocchino
commented
Mar 9, 2026
lib/m3u8/segment_item.rb
Outdated
| when Float | ||
| BigDecimal(number).to_s('F') | ||
| else | ||
| number.to_s |
Contributor
Author
There was a problem hiding this comment.
> 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.
marocchino
commented
Mar 9, 2026
lib/m3u8/segment_item.rb
Outdated
| def float_format(number) | ||
| case duration | ||
| when Float | ||
| BigDecimal(number).to_s('F') |
Contributor
Author
There was a problem hiding this comment.
$ 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
Owner
|
Hi @marocchino thank you for the PR, would you please address the following?
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. |
fce02cd to
1c02215
Compare
4a96d39 to
081031b
Compare
Merged
Owner
|
@marocchino released in 1.8.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
close #58