mirror of
https://github.com/FlipsideCrypto/dcrd.git
synced 2026-02-06 10:56:47 +00:00
This merge commit adds the following code from the github.com/decred/dcrutil package into a new github.com/decred/dcrd/dcrutil package: * Address handling * Amount type * AppDataDir func * bitflags functions * Block wrapper type * Hash160 func * Tx wrapper type * WIF type as well as all tests for this code. The old github.com/decred/dcrutil/hdkeychain package has also been merged and moved to github.com/decred/dcrd/dcrutil/hdkeychain. dcrd packages have been updated to use the new packages and the dep files have been updated for this change.
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package dcrutil_test
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
"github.com/decred/dcrd/dcrutil"
|
|
)
|
|
|
|
func ExampleAmount() {
|
|
|
|
a := dcrutil.Amount(0)
|
|
fmt.Println("Zero Atom:", a)
|
|
|
|
a = dcrutil.Amount(1e8)
|
|
fmt.Println("100,000,000 Atoms:", a)
|
|
|
|
a = dcrutil.Amount(1e5)
|
|
fmt.Println("100,000 Atoms:", a)
|
|
// Output:
|
|
// Zero Atom: 0 DCR
|
|
// 100,000,000 Atoms: 1 DCR
|
|
// 100,000 Atoms: 0.001 DCR
|
|
}
|
|
|
|
func ExampleNewAmount() {
|
|
amountOne, err := dcrutil.NewAmount(1)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountOne) //Output 1
|
|
|
|
amountFraction, err := dcrutil.NewAmount(0.01234567)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountFraction) //Output 2
|
|
|
|
amountZero, err := dcrutil.NewAmount(0)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountZero) //Output 3
|
|
|
|
amountNaN, err := dcrutil.NewAmount(math.NaN())
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(amountNaN) //Output 4
|
|
|
|
// Output: 1 DCR
|
|
// 0.01234567 DCR
|
|
// 0 DCR
|
|
// invalid coin amount
|
|
}
|
|
|
|
func ExampleAmount_unitConversions() {
|
|
amount := dcrutil.Amount(44433322211100)
|
|
|
|
fmt.Println("Atom to kCoin:", amount.Format(dcrutil.AmountKiloCoin))
|
|
fmt.Println("Atom to Coin:", amount)
|
|
fmt.Println("Atom to MilliCoin:", amount.Format(dcrutil.AmountMilliCoin))
|
|
fmt.Println("Atom to MicroCoin:", amount.Format(dcrutil.AmountMicroCoin))
|
|
fmt.Println("Atom to Atom:", amount.Format(dcrutil.AmountAtom))
|
|
|
|
// Output:
|
|
// Atom to kCoin: 444.333222111 kDCR
|
|
// Atom to Coin: 444333.222111 DCR
|
|
// Atom to MilliCoin: 444333222.111 mDCR
|
|
// Atom to MicroCoin: 444333222111 μDCR
|
|
// Atom to Atom: 44433322211100 Atom
|
|
}
|