decimal/README.md

68 lines
1.6 KiB
Markdown
Raw Normal View History

2015-02-25 18:33:53 +01:00
# decimal [![Build Status](https://travis-ci.org/shopspring/decimal.png?branch=master)](https://travis-ci.org/shopspring/decimal)
2015-02-10 21:33:58 +01:00
Arbitrary-precision fixed-point decimal numbers in go.
NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decmial point.
2015-02-10 22:49:10 +01:00
## Features
2015-02-25 05:11:00 +01:00
* the zero-value is 0, and is safe to use without initialization
2015-02-10 22:49:10 +01:00
* addition, subtraction, multiplication with no loss of precision
* division with specified precision
* database/sql serialization/deserialization
* json and xml serialization/deserialization
2015-02-25 05:11:00 +01:00
## Install
2015-02-10 22:49:10 +01:00
2015-02-25 18:33:53 +01:00
Run `go get github.com/shopspring/decimal`
2015-02-10 22:49:10 +01:00
2015-02-10 21:33:58 +01:00
## Usage
```go
package main
import (
"fmt"
2015-02-25 18:33:53 +01:00
"github.com/shopspring/decimal"
2015-02-10 21:33:58 +01:00
)
func main() {
price, err := decimal.NewFromString("136.02")
if err != nil {
panic(err)
}
quantity := decimal.NewFromFloat(3)
fee, _ := decimal.NewFromString(".035")
taxRate, _ := decimal.NewFromString(".08875")
subtotal := price.Mul(quantity)
preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))
total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))
fmt.Println("Subtotal:", subtotal)
fmt.Println("Pre-tax:", preTax)
fmt.Println("Taxes:", total.Sub(preTax))
fmt.Println("Total:", total)
fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax))
}
```
## Documentation
2015-02-25 18:33:53 +01:00
http://godoc.org/github.com/shopspring/decimal
2015-02-10 21:33:58 +01:00
2015-02-25 05:11:00 +01:00
## Production Usage
* [Spring](https://shopspring.com/), since August 14, 2014.
* If you are using this in production, please let us know!
2015-02-10 21:33:58 +01:00
## License
The MIT License (MIT)
This is a heavily modified fork of [fpd.Decimal](https://github.com/oguzbilgic/fpd), which was also released under the MIT License.