# decimal
[![Build Status](https://travis-ci.org/shopspring/decimal.png?branch=master)](https://travis-ci.org/shopspring/decimal) [![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal) [![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal)
Arbitrary-precision fixed-point decimal numbers in go.
NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
## Features
* the zero-value is 0, and is safe to use without initialization
* addition, subtraction, multiplication with no loss of precision
* division with specified precision
* database/sql serialization/deserialization
* json and xml serialization/deserialization
## Install
Run `go get github.com/shopspring/decimal`
## Usage
```go
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
price, err := decimal.NewFromString("136.02")
if err != nil {
panic(err)
}
quantity := decimal.NewFromInt(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) // Subtotal: 408.06
fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421
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
}
```
## Documentation
http://godoc.org/github.com/shopspring/decimal
## Production Usage
* [Spring](https://shopspring.com/), since August 14, 2014.
* If you are using this in production, please let us know!
## FAQ
#### Why don't you just use float64?
Because float64s (or any binary floating point type, actually) can't represent
numbers such as 0.1 exactly.
Consider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that
it prints out `10`, but it actually prints `9.999999999999831`. Over time,
these small errors can really add up!
#### Why don't you just use big.Rat?
big.Rat is fine for representing rational numbers, but Decimal is better for
representing money. Why? Here's a (contrived) example:
Let's say you use big.Rat, and you have two numbers, x and y, both
representing 1/3, and you have `z = 1 - x - y = 1/3`. If you print each one
out, the string output has to stop somewhere (let's say it stops at 3 decimal
digits, for simplicity), so you'll get 0.333, 0.333, and 0.333. But where did
the other 0.001 go?
Here's the above example as code: http://play.golang.org/p/lCZZs0w9KE
With Decimal, the strings being printed out represent the number exactly. So,
if you have `x = y = 1/3` (with precision 3), they will actually be equal to
0.333, and when you do `z = 1 - x - y`, `z` will be equal to .334. No money is
unaccounted for!
You still have to be careful. If you want to split a number `N` 3 ways, you
can't just send `N/3` to three different people. You have to pick one to send
`N - (2/3*N)` to. That person will receive the fraction of a penny remainder.
But, it is much easier to be careful with Decimal than with big.Rat.
#### Why isn't the API similar to big.Int's?
big.Int's API is built to reduce the number of memory allocations for maximal
performance. This makes sense for its use-case, but the trade-off is that the
API is awkward and easy to misuse.
For example, to add two big.Ints, you do: `z := new(big.Int).Add(x, y)`. A
developer unfamiliar with this API might try to do `z := a.Add(a, b)`. This
modifies `a` and sets `z` as an alias for `a`, which they might not expect. It
also modifies any other aliases to `a`.
Here's an example of the subtle bugs you can introduce with big.Int's API:
https://play.golang.org/p/x2R_78pa8r
In contrast, it's difficult to make such mistakes with decimal. Decimals
behave like other go numbers types: even though `a = b` will not deep copy
`b` into `a`, it is impossible to modify a Decimal, since all Decimal methods
return new Decimals and do not modify the originals. The downside is that
this causes extra allocations, so Decimal is less performant. My assumption
is that if you're using Decimals, you probably care more about correctness
than performance.
## 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.
没有合适的资源?快使用搜索试试~ 我知道了~
00-Golang教程 课件+源码 -大地老师.zip
共211个文件
go:169个
pdf:23个
mod:8个
需积分: 0 0 下载量 40 浏览量
2024-05-16
01:42:34
上传
评论
收藏 5.71MB ZIP 举报
温馨提示
00-Golang教程 课件+源码 -大地老师.zip
资源推荐
资源详情
资源评论
收起资源包目录
00-Golang教程 课件+源码 -大地老师.zip (211个子文件)
.gitignore 36B
decimal.go 38KB
decimal-go.go 11KB
main.go 4KB
rounding.go 4KB
main.go 3KB
main.go 3KB
main.go 3KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 2KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1KB
main.go 1024B
main.go 1023B
main.go 1014B
main.go 1008B
main.go 1005B
main.go 1003B
main.go 999B
main.go 971B
main.go 958B
main.go 957B
main.go 956B
main.go 952B
main.go 948B
main.go 934B
main.go 932B
main.go 925B
main.go 912B
main.go 909B
main.go 886B
main.go 886B
main.go 862B
main.go 853B
main.go 852B
main.go 837B
main.go 836B
main.go 836B
main.go 834B
main.go 816B
main.go 815B
main.go 813B
main.go 812B
main.go 810B
main.go 797B
main.go 793B
main.go 789B
main.go 783B
main.go 773B
main.go 767B
main.go 766B
main.go 759B
main.go 753B
main.go 746B
main.go 721B
main.go 715B
main.go 714B
main.go 713B
main.go 709B
main.go 709B
main.go 702B
main.go 702B
main.go 687B
main.go 684B
main.go 684B
共 211 条
- 1
- 2
- 3
资源评论
m0_74949141
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功