diff --git a/btcutil/amount.go b/btcutil/amount.go index 71714153aa..bb70c9b11d 100644 --- a/btcutil/amount.go +++ b/btcutil/amount.go @@ -6,8 +6,10 @@ package btcutil import ( "errors" + "fmt" "math" "strconv" + "strings" ) // AmountUnit describes a method of converting an Amount to something @@ -101,11 +103,20 @@ func (a Amount) ToBTC() float64 { // 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 +// however, known units will be formatted with an appended label describing // the units with SI notation, or "Satoshi" for the base unit. func (a Amount) Format(u AmountUnit) string { units := " " + u.String() - return strconv.FormatFloat(a.ToUnit(u), 'f', -int(u+8), 64) + units + formatted := strconv.FormatFloat(a.ToUnit(u), 'f', -int(u+8), 64) + + // When formatting full BTC, add trailing zeroes for numbers + // with decimal point to ease reading of sat amount. + if u == AmountBTC { + if strings.Contains(formatted, ".") { + return fmt.Sprintf("%.8f%s", a.ToUnit(u), units) + } + } + return formatted + units } // String is the equivalent of calling Format with AmountBTC. diff --git a/btcutil/amount_test.go b/btcutil/amount_test.go index 2b6c3f753d..69498b07e2 100644 --- a/btcutil/amount_test.go +++ b/btcutil/amount_test.go @@ -136,8 +136,29 @@ func TestAmountUnitConversions(t *testing.T) { name: "BTC", amount: 44433322211100, unit: AmountBTC, - converted: 444333.22211100, - s: "444333.222111 BTC", + converted: 444333.222111, + s: "444333.22211100 BTC", + }, + { + name: "a thousand satoshi as BTC", + amount: 1000, + unit: AmountBTC, + converted: 0.00001, + s: "0.00001000 BTC", + }, + { + name: "a single satoshi as BTC", + amount: 1, + unit: AmountBTC, + converted: 0.00000001, + s: "0.00000001 BTC", + }, + { + name: "amount with trailing zero but no decimals", + amount: 1000000000, + unit: AmountBTC, + converted: 10, + s: "10 BTC", }, { name: "mBTC", diff --git a/btcutil/example_test.go b/btcutil/example_test.go index 6b62fdd44f..90be77b073 100644 --- a/btcutil/example_test.go +++ b/btcutil/example_test.go @@ -20,7 +20,7 @@ func ExampleAmount() { // Output: // Zero Satoshi: 0 BTC // 100,000,000 Satoshis: 1 BTC - // 100,000 Satoshis: 0.001 BTC + // 100,000 Satoshis: 0.00100000 BTC } func ExampleNewAmount() { @@ -69,7 +69,7 @@ func ExampleAmount_unitConversions() { // Output: // Satoshi to kBTC: 444.333222111 kBTC - // Satoshi to BTC: 444333.222111 BTC + // Satoshi to BTC: 444333.22211100 BTC // Satoshi to MilliBTC: 444333222.111 mBTC // Satoshi to MicroBTC: 444333222111 μBTC // Satoshi to Satoshi: 44433322211100 Satoshi