Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from kitcheck/update_formatter
Browse files Browse the repository at this point in the history
Formatter to strip trailing zeroes
  • Loading branch information
Alex Johnson committed Feb 23, 2016
2 parents 9a09abe + 6840764 commit 675aad7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/unit/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Unit
module Formatter
def to_s
"#{scalar.to_s("F")} #{uom}"
"#{("%g" % scalar)} #{uom}"
end

def to_hash
Expand Down
11 changes: 6 additions & 5 deletions test/display_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 675aad7

Please sign in to comment.