mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
format usage code in README (#70)
This commit is contained in:
parent
9ca7f51822
commit
8dffa2b7c7
1 changed files with 9 additions and 9 deletions
18
README.md
18
README.md
|
@ -24,30 +24,30 @@ Run `go get github.com/shopspring/decimal`
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/shopspring/decimal"
|
||||
"fmt"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func main() {
|
||||
price, err := decimal.NewFromString("136.02")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
quantity := decimal.NewFromFloat(3)
|
||||
|
||||
fee, _ := decimal.NewFromString(".035")
|
||||
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("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("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue