From ba47b1d41949beab959affc7497e4120d3a9c8a0 Mon Sep 17 00:00:00 2001 From: Kevin Olbrich <22176+olbrich@users.noreply.github.com> Date: Sat, 23 Mar 2024 10:11:33 -0400 Subject: [PATCH] round/ceil/floor for centi prefix not working (#344) --- lib/ruby_units/unit.rb | 7 +++++-- spec/ruby_units/unit_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/ruby_units/unit.rb b/lib/ruby_units/unit.rb index b2b01da..3af26a4 100644 --- a/lib/ruby_units/unit.rb +++ b/lib/ruby_units/unit.rb @@ -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 into ... + while unit_string.gsub!(/<(#{self.class.prefix_regex})><(#{self.class.unit_regex})>/, '<\1\2>') + # replace with + end + while unit_string.gsub!(/<#{self.class.unit_match_regex}><#{self.class.unit_match_regex}>/, '<\1\2>*<\3\4>') + # collapse into *... end # ... and then strip the remaining brackets for x*y*z unit_string.gsub!(/[<>]/, '') diff --git a/spec/ruby_units/unit_spec.rb b/spec/ruby_units/unit_spec.rb index 80b821b..696076c 100644 --- a/spec/ruby_units/unit_spec.rb +++ b/spec/ruby_units/unit_spec.rb @@ -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