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

Formatter to strip trailing zeroes #51

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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