diff --git a/lib/unit/formatter.rb b/lib/unit/formatter.rb index 72b1688..e7a35e2 100644 --- a/lib/unit/formatter.rb +++ b/lib/unit/formatter.rb @@ -1,7 +1,7 @@ module Unit module Formatter def to_s - "#{scalar.to_s("F")} #{uom}" + "#{("%g" % scalar)} #{uom}" end def to_hash diff --git a/test/display_test.rb b/test/display_test.rb index cf34475..57eaebd 100644 --- a/test/display_test.rb +++ b/test/display_test.rb @@ -3,11 +3,12 @@ class DisplayTest < Minitest::Test context "#to_s" do [ - [Unit.parse('5 mg'), "5.0 mg"], - [Unit.parse('5 ml'), "5.0 ml"], - [Unit.parse('5 unit'), "5.0 unit"], - [Unit.parse('5 mg/ml'), "5.0 mg/ml"], - [Unit.parse('5 meq'), "5.0 meq"] + [Unit.parse('5 mg'), "5 mg"], + [Unit.parse('5.5 mg'), "5.5 mg"], + [Unit.parse('5 ml'), "5 ml"], + [Unit.parse('5 unit'), "5 unit"], + [Unit.parse('5 mg/ml'), "5 mg/ml"], + [Unit.parse('5 meq'), "5 meq"] ].each do |unit, expected| should "translate a unit with scalar #{unit.scalar} and uom #{unit.uom} to #{expected}" do assert_equal unit.to_s, expected