Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btcutil: format BTC amounts with trailing zeroes #1996

Merged
merged 1 commit into from
Jul 10, 2023
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
15 changes: 13 additions & 2 deletions btcutil/amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package btcutil

import (
"errors"
"fmt"
"math"
"strconv"
"strings"
)

// AmountUnit describes a method of converting an Amount to something
Expand Down Expand Up @@ -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.
Expand Down
25 changes: 23 additions & 2 deletions btcutil/amount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
},
{
name: "amount with trailing zero but no decimals",
amount: 1000000000,
unit: AmountBTC,
converted: 10,
s: "10 BTC",
},
{
name: "mBTC",
Expand Down
4 changes: 2 additions & 2 deletions btcutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down