2015-07-20 18:00:32 +02:00
|
|
|
package btcutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2015-07-21 14:49:31 +02:00
|
|
|
"github.com/btcsuite/btcutil"
|
2015-07-20 18:00:32 +02:00
|
|
|
)
|
|
|
|
|
2015-07-21 14:49:31 +02:00
|
|
|
func ExampleAmount() {
|
|
|
|
|
|
|
|
a := btcutil.Amount(0)
|
|
|
|
fmt.Println("Zero Satoshi:", a)
|
|
|
|
|
|
|
|
a = btcutil.Amount(1e8)
|
|
|
|
fmt.Println("100,000,000 Satoshis:", a)
|
|
|
|
|
|
|
|
a = btcutil.Amount(1e5)
|
|
|
|
fmt.Println("100,000 Satoshis:", a)
|
|
|
|
// Output:
|
|
|
|
// Zero Satoshi: 0 BTC
|
|
|
|
// 100,000,000 Satoshis: 1 BTC
|
|
|
|
// 100,000 Satoshis: 0.001 BTC
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:00:32 +02:00
|
|
|
func ExampleNewAmount() {
|
2015-07-21 14:49:31 +02:00
|
|
|
amountOne, err := btcutil.NewAmount(1)
|
2015-07-20 18:00:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(amountOne) //Output 1
|
|
|
|
|
2015-07-21 14:49:31 +02:00
|
|
|
amountFraction, err := btcutil.NewAmount(0.01234567)
|
2015-07-20 18:00:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(amountFraction) //Output 2
|
|
|
|
|
2015-07-21 14:49:31 +02:00
|
|
|
amountZero, err := btcutil.NewAmount(0)
|
2015-07-20 18:00:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(amountZero) //Output 3
|
|
|
|
|
2015-07-21 14:49:31 +02:00
|
|
|
amountNaN, err := btcutil.NewAmount(math.NaN())
|
2015-07-20 18:00:32 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(amountNaN) //Output 4
|
|
|
|
|
|
|
|
// Output: 1 BTC
|
|
|
|
// 0.01234567 BTC
|
|
|
|
// 0 BTC
|
|
|
|
// invalid bitcoin amount
|
|
|
|
}
|
2015-07-21 14:49:31 +02:00
|
|
|
|
|
|
|
func ExampleAmount_unitConversions() {
|
|
|
|
amount := btcutil.Amount(44433322211100)
|
|
|
|
|
|
|
|
fmt.Println("Satoshi to kBTC:", amount.Format(btcutil.AmountKiloBTC))
|
|
|
|
fmt.Println("Satoshi to BTC:", amount)
|
|
|
|
fmt.Println("Satoshi to MilliBTC:", amount.Format(btcutil.AmountMilliBTC))
|
|
|
|
fmt.Println("Satoshi to MicroBTC:", amount.Format(btcutil.AmountMicroBTC))
|
|
|
|
fmt.Println("Satoshi to Satoshi:", amount.Format(btcutil.AmountSatoshi))
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Satoshi to kBTC: 444.333222111 kBTC
|
|
|
|
// Satoshi to BTC: 444333.222111 BTC
|
|
|
|
// Satoshi to MilliBTC: 444333222.111 mBTC
|
|
|
|
// Satoshi to MicroBTC: 444333222111 μBTC
|
|
|
|
// Satoshi to Satoshi: 44433322211100 Satoshi
|
|
|
|
}
|