Rename AmountUnit constants.
Besides being shorter, using "BTC" rather than "Bitcoin" in the AmountUnit constants is deemed to be better for these units as BTC is already a recognized monetary unit. AmountBaseBitcoin has likewise been renamed to AmountSatoshi as this is consistant with how it is returned as a string. The "standard" part of the comment in the const block has been removed, as Satoshi is technically not a standard term for this unit. ok @davecgh
This commit is contained in:
parent
80b1f232bc
commit
3a2bf60941
2 changed files with 24 additions and 24 deletions
34
amount.go
34
amount.go
|
@ -16,15 +16,15 @@ import (
|
||||||
// an amount in bitcoin to an amount counted in units.
|
// an amount in bitcoin to an amount counted in units.
|
||||||
type AmountUnit int
|
type AmountUnit int
|
||||||
|
|
||||||
// These constants define the various standard units used when describing
|
// These constants define various units used when describing a bitcoin
|
||||||
// a bitcoin monetary amount.
|
// monetary amount.
|
||||||
const (
|
const (
|
||||||
AmountMegaBitcoin AmountUnit = 6
|
AmountMegaBTC AmountUnit = 6
|
||||||
AmountKiloBitcoin AmountUnit = 3
|
AmountKiloBTC AmountUnit = 3
|
||||||
AmountBitcoin AmountUnit = 0
|
AmountBTC AmountUnit = 0
|
||||||
AmountMilliBitcoin AmountUnit = -3
|
AmountMilliBTC AmountUnit = -3
|
||||||
AmountMicroBitcoin AmountUnit = -6
|
AmountMicroBTC AmountUnit = -6
|
||||||
AmountBaseBitcoin AmountUnit = -8
|
AmountSatoshi AmountUnit = -8
|
||||||
)
|
)
|
||||||
|
|
||||||
// String returns the unit as a string. For recognized units, the SI
|
// String returns the unit as a string. For recognized units, the SI
|
||||||
|
@ -32,17 +32,17 @@ const (
|
||||||
// units, "1eN BTC" is returned, where N is the AmountUnit.
|
// units, "1eN BTC" is returned, where N is the AmountUnit.
|
||||||
func (u AmountUnit) String() string {
|
func (u AmountUnit) String() string {
|
||||||
switch u {
|
switch u {
|
||||||
case AmountMegaBitcoin:
|
case AmountMegaBTC:
|
||||||
return "MBTC"
|
return "MBTC"
|
||||||
case AmountKiloBitcoin:
|
case AmountKiloBTC:
|
||||||
return "kBTC"
|
return "kBTC"
|
||||||
case AmountBitcoin:
|
case AmountBTC:
|
||||||
return "BTC"
|
return "BTC"
|
||||||
case AmountMilliBitcoin:
|
case AmountMilliBTC:
|
||||||
return "mBTC"
|
return "mBTC"
|
||||||
case AmountMicroBitcoin:
|
case AmountMicroBTC:
|
||||||
return "μBTC"
|
return "μBTC"
|
||||||
case AmountBaseBitcoin:
|
case AmountSatoshi:
|
||||||
return "Satoshi"
|
return "Satoshi"
|
||||||
default:
|
default:
|
||||||
return "1e" + strconv.FormatInt(int64(u), 10) + " BTC"
|
return "1e" + strconv.FormatInt(int64(u), 10) + " BTC"
|
||||||
|
@ -87,13 +87,13 @@ func (a Amount) ToUnit(u AmountUnit) float64 {
|
||||||
// Format formats a monetary amount counted in bitcoin base units as a
|
// Format formats a monetary amount counted in bitcoin base units as a
|
||||||
// string for a given unit. The conversion will succeed for any unit,
|
// 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 formated with an appended label describing
|
||||||
// the units with SI notation.
|
// the units with SI notation, or "Satoshi" for the base unit.
|
||||||
func (a Amount) Format(u AmountUnit) string {
|
func (a Amount) Format(u AmountUnit) string {
|
||||||
units := " " + u.String()
|
units := " " + u.String()
|
||||||
return strconv.FormatFloat(a.ToUnit(u), 'f', -int(u+8), 64) + units
|
return strconv.FormatFloat(a.ToUnit(u), 'f', -int(u+8), 64) + units
|
||||||
}
|
}
|
||||||
|
|
||||||
// String is the equivalent of calling Format with AmountBitcoin.
|
// String is the equivalent of calling Format with AmountBTC.
|
||||||
func (a Amount) String() string {
|
func (a Amount) String() string {
|
||||||
return a.Format(AmountBitcoin)
|
return a.Format(AmountBTC)
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,28 +118,28 @@ func TestAmountUnitConversions(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "MBTC",
|
name: "MBTC",
|
||||||
amount: Amount(MaxSatoshi),
|
amount: Amount(MaxSatoshi),
|
||||||
unit: AmountMegaBitcoin,
|
unit: AmountMegaBTC,
|
||||||
converted: 21,
|
converted: 21,
|
||||||
s: "21 MBTC",
|
s: "21 MBTC",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "kBTC",
|
name: "kBTC",
|
||||||
amount: Amount(44433322211100),
|
amount: Amount(44433322211100),
|
||||||
unit: AmountKiloBitcoin,
|
unit: AmountKiloBTC,
|
||||||
converted: 444.33322211100,
|
converted: 444.33322211100,
|
||||||
s: "444.333222111 kBTC",
|
s: "444.333222111 kBTC",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "BTC",
|
name: "BTC",
|
||||||
amount: Amount(44433322211100),
|
amount: Amount(44433322211100),
|
||||||
unit: AmountBitcoin,
|
unit: AmountBTC,
|
||||||
converted: 444333.22211100,
|
converted: 444333.22211100,
|
||||||
s: "444333.222111 BTC",
|
s: "444333.222111 BTC",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mBTC",
|
name: "mBTC",
|
||||||
amount: Amount(44433322211100),
|
amount: Amount(44433322211100),
|
||||||
unit: AmountMilliBitcoin,
|
unit: AmountMilliBTC,
|
||||||
converted: 444333222.11100,
|
converted: 444333222.11100,
|
||||||
s: "444333222.111 mBTC",
|
s: "444333222.111 mBTC",
|
||||||
},
|
},
|
||||||
|
@ -147,7 +147,7 @@ func TestAmountUnitConversions(t *testing.T) {
|
||||||
|
|
||||||
name: "μBTC",
|
name: "μBTC",
|
||||||
amount: Amount(44433322211100),
|
amount: Amount(44433322211100),
|
||||||
unit: AmountMicroBitcoin,
|
unit: AmountMicroBTC,
|
||||||
converted: 444333222111.00,
|
converted: 444333222111.00,
|
||||||
s: "444333222111 μBTC",
|
s: "444333222111 μBTC",
|
||||||
},
|
},
|
||||||
|
@ -155,7 +155,7 @@ func TestAmountUnitConversions(t *testing.T) {
|
||||||
|
|
||||||
name: "satoshi",
|
name: "satoshi",
|
||||||
amount: Amount(44433322211100),
|
amount: Amount(44433322211100),
|
||||||
unit: AmountBaseBitcoin,
|
unit: AmountSatoshi,
|
||||||
converted: 44433322211100,
|
converted: 44433322211100,
|
||||||
s: "44433322211100 Satoshi",
|
s: "44433322211100 Satoshi",
|
||||||
},
|
},
|
||||||
|
@ -183,7 +183,7 @@ func TestAmountUnitConversions(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that Amount.String works as advertised.
|
// Verify that Amount.String works as advertised.
|
||||||
s1 := test.amount.Format(AmountBitcoin)
|
s1 := test.amount.Format(AmountBTC)
|
||||||
s2 := test.amount.String()
|
s2 := test.amount.String()
|
||||||
if s1 != s2 {
|
if s1 != s2 {
|
||||||
t.Errorf("%v: String does not match Format(AmountBitcoin): %v != %v", test.name, s1, s2)
|
t.Errorf("%v: String does not match Format(AmountBitcoin): %v != %v", test.name, s1, s2)
|
||||||
|
|
Loading…
Reference in a new issue