diff --git a/btcutil/amount.go b/btcutil/amount.go index 71714153aa4..4f46bbc001b 100644 --- a/btcutil/amount.go +++ b/btcutil/amount.go @@ -8,6 +8,7 @@ import ( "errors" "math" "strconv" + "strings" ) // AmountUnit describes a method of converting an Amount to something @@ -101,11 +102,24 @@ 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 + bitcoin := a.ToUnit(u) + formatted := strconv.FormatFloat(bitcoin, '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 { + decimalPoint := "." + before, after, containsDecimalPoint := strings.Cut(formatted, decimalPoint) + if containsDecimalPoint { + trailingZeroes := strings.Repeat("0", 8-len(after)) + return before + decimalPoint + after + trailingZeroes + 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 2b6c3f753df..921acc134de 100644 --- a/btcutil/amount_test.go +++ b/btcutil/amount_test.go @@ -136,8 +136,22 @@ 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: "mBTC", diff --git a/btcutil/example_test.go b/btcutil/example_test.go index 6b62fdd44f2..90be77b073c 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