Skip to content

Commit

Permalink
round/ceil/floor for centi prefix not working (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
olbrich authored Mar 23, 2024
1 parent 89fb3a8 commit ba47b1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1640,8 +1640,11 @@ def parse(passed_unit_string = '0')
return self
end

while unit_string.gsub!(/(<#{self.class.unit_regex})><(#{self.class.unit_regex}>)/, '\1*\2')
# collapse <x><y><z> into <x*y*z>...
while unit_string.gsub!(/<(#{self.class.prefix_regex})><(#{self.class.unit_regex})>/, '<\1\2>')
# replace <prefix><unit> with <prefixunit>
end
while unit_string.gsub!(/<#{self.class.unit_match_regex}><#{self.class.unit_match_regex}>/, '<\1\2>*<\3\4>')
# collapse <prefixunit><prefixunit> into <prefixunit>*<prefixunit>...
end
# ... and then strip the remaining brackets for x*y*z
unit_string.gsub!(/[<>]/, '')
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby_units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,14 @@
expect(unit.round(3, half: :down)).to eq(RubyUnits::Unit.new('1.234 m'))
end
end

context 'with a unit containing a centi-prefix' do
subject(:unit) { RubyUnits::Unit.new('1.2345 cm^2') }

it 'rounds correctly for squared unit' do
expect(unit.round).to eq(RubyUnits::Unit.new('1 cm^2'))
end
end
end

context '#truncate' do
Expand Down

0 comments on commit ba47b1d

Please sign in to comment.