From 40ba1daf690afbd356066e36c47ba0e4d071ddb0 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Fri, 2 Jan 2015 09:19:16 -0500 Subject: [PATCH] Add Amount.ToBTC convenience method. Closes #26. --- amount.go | 5 +++++ amount_test.go | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/amount.go b/amount.go index 025f4d9..23e53bb 100644 --- a/amount.go +++ b/amount.go @@ -89,6 +89,11 @@ func (a Amount) ToUnit(u AmountUnit) float64 { return float64(a) / math.Pow10(int(u+8)) } +// ToBTC is the equivalent of calling ToUnit with AmountBTC. +func (a Amount) ToBTC() float64 { + return a.ToUnit(AmountBTC) +} + // Format formats a monetary amount counted in bitcoin base units as a // string for a given unit. The conversion will succeed for any unit, // however, known units will be formated with an appended label describing diff --git a/amount_test.go b/amount_test.go index 0ceff6a..7bc5e75 100644 --- a/amount_test.go +++ b/amount_test.go @@ -185,6 +185,13 @@ func TestAmountUnitConversions(t *testing.T) { continue } + // Verify that Amount.ToBTC works as advertised. + f1 := test.amount.ToUnit(AmountBTC) + f2 := test.amount.ToBTC() + if f1 != f2 { + t.Errorf("%v: ToBTC does not match ToUnit(AmountBTC): %v != %v", test.name, f1, f2) + } + // Verify that Amount.String works as advertised. s1 := test.amount.Format(AmountBTC) s2 := test.amount.String()