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:
Josh Rickmar 2014-04-11 23:23:27 -05:00
parent 80b1f232bc
commit 3a2bf60941
2 changed files with 24 additions and 24 deletions

View file

@ -16,15 +16,15 @@ import (
// an amount in bitcoin to an amount counted in units.
type AmountUnit int
// These constants define the various standard units used when describing
// a bitcoin monetary amount.
// These constants define various units used when describing a bitcoin
// monetary amount.
const (
AmountMegaBitcoin AmountUnit = 6
AmountKiloBitcoin AmountUnit = 3
AmountBitcoin AmountUnit = 0
AmountMilliBitcoin AmountUnit = -3
AmountMicroBitcoin AmountUnit = -6
AmountBaseBitcoin AmountUnit = -8
AmountMegaBTC AmountUnit = 6
AmountKiloBTC AmountUnit = 3
AmountBTC AmountUnit = 0
AmountMilliBTC AmountUnit = -3
AmountMicroBTC AmountUnit = -6
AmountSatoshi AmountUnit = -8
)
// 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.
func (u AmountUnit) String() string {
switch u {
case AmountMegaBitcoin:
case AmountMegaBTC:
return "MBTC"
case AmountKiloBitcoin:
case AmountKiloBTC:
return "kBTC"
case AmountBitcoin:
case AmountBTC:
return "BTC"
case AmountMilliBitcoin:
case AmountMilliBTC:
return "mBTC"
case AmountMicroBitcoin:
case AmountMicroBTC:
return "μBTC"
case AmountBaseBitcoin:
case AmountSatoshi:
return "Satoshi"
default:
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
// string for a given unit. The conversion will succeed for any unit,
// 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 {
units := " " + u.String()
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 {
return a.Format(AmountBitcoin)
return a.Format(AmountBTC)
}

View file

@ -118,28 +118,28 @@ func TestAmountUnitConversions(t *testing.T) {
{
name: "MBTC",
amount: Amount(MaxSatoshi),
unit: AmountMegaBitcoin,
unit: AmountMegaBTC,
converted: 21,
s: "21 MBTC",
},
{
name: "kBTC",
amount: Amount(44433322211100),
unit: AmountKiloBitcoin,
unit: AmountKiloBTC,
converted: 444.33322211100,
s: "444.333222111 kBTC",
},
{
name: "BTC",
amount: Amount(44433322211100),
unit: AmountBitcoin,
unit: AmountBTC,
converted: 444333.22211100,
s: "444333.222111 BTC",
},
{
name: "mBTC",
amount: Amount(44433322211100),
unit: AmountMilliBitcoin,
unit: AmountMilliBTC,
converted: 444333222.11100,
s: "444333222.111 mBTC",
},
@ -147,7 +147,7 @@ func TestAmountUnitConversions(t *testing.T) {
name: "μBTC",
amount: Amount(44433322211100),
unit: AmountMicroBitcoin,
unit: AmountMicroBTC,
converted: 444333222111.00,
s: "444333222111 μBTC",
},
@ -155,7 +155,7 @@ func TestAmountUnitConversions(t *testing.T) {
name: "satoshi",
amount: Amount(44433322211100),
unit: AmountBaseBitcoin,
unit: AmountSatoshi,
converted: 44433322211100,
s: "44433322211100 Satoshi",
},
@ -183,7 +183,7 @@ func TestAmountUnitConversions(t *testing.T) {
}
// Verify that Amount.String works as advertised.
s1 := test.amount.Format(AmountBitcoin)
s1 := test.amount.Format(AmountBTC)
s2 := test.amount.String()
if s1 != s2 {
t.Errorf("%v: String does not match Format(AmountBitcoin): %v != %v", test.name, s1, s2)