format usage code in README (#70)

This commit is contained in:
Zheng Dayu 2018-01-02 13:16:03 -06:00 committed by Victor Quinn
parent 9ca7f51822
commit 8dffa2b7c7

View file

@ -24,30 +24,30 @@ Run `go get github.com/shopspring/decimal`
package main package main
import ( import (
"fmt" "fmt"
"github.com/shopspring/decimal" "github.com/shopspring/decimal"
) )
func main() { func main() {
price, err := decimal.NewFromString("136.02") price, err := decimal.NewFromString("136.02")
if err != nil { if err != nil {
panic(err) panic(err)
} }
quantity := decimal.NewFromFloat(3) quantity := decimal.NewFromFloat(3)
fee, _ := decimal.NewFromString(".035") fee, _ := decimal.NewFromString(".035")
taxRate, _ := decimal.NewFromString(".08875") taxRate, _ := decimal.NewFromString(".08875")
subtotal := price.Mul(quantity) subtotal := price.Mul(quantity)
preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1))) preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))
total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1))) total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))
fmt.Println("Subtotal:", subtotal) // Subtotal: 408.06 fmt.Println("Subtotal:", subtotal) // Subtotal: 408.06
fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421 fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421
fmt.Println("Taxes:", total.Sub(preTax)) // Taxes: 37.482861375 fmt.Println("Taxes:", total.Sub(preTax)) // Taxes: 37.482861375
fmt.Println("Total:", total) // Total: 459.824961375 fmt.Println("Total:", total) // Total: 459.824961375
fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875 fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875
} }