Skip to content

Commit 5c367dd

Browse files
authored
Merge pull request #674 from devlights:add-floatop-using-decimal-pkg
Add floatop/using_decimal_pkg.go
2 parents 5f4e276 + 9a99047 commit 5c367dd

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

examples/basic/floatop/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
|----|------------|----|
77
|order\_of\_computation.go|floatop\_order\_of\_computation|浮動小数点は計算の順序によって結果が変わることのサンプルです|
88
|rounding\_error.go|floatop\_rounding\_error|小数点計算において近似値が利用され丸め誤差が出るサンプルです|
9+
|using\_decimal\_pkg.go|floatop\_using\_decimal\_pkg|小数点計算を github.com/shopspring/decimal パッケージを利用して処理するサンプルです|

examples/basic/floatop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
1515
func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["floatop_order_of_computation"] = OrderOfComputation
1717
m["floatop_rounding_error"] = RoundingError
18+
m["floatop_using_decimal_pkg"] = UsingDecimalPkg
1819
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package floatop
2+
3+
import (
4+
"github.com/devlights/gomy/output"
5+
"github.com/shopspring/decimal"
6+
)
7+
8+
// UsingDecimalPkg は、小数点計算を github.com/shopspring/decimal パッケージを利用して処理するサンプルです。
9+
//
10+
// # REFERENCES
11+
// - https://engineering.mercari.com/blog/entry/20201203-basis-point/
12+
// - https://github.com/shopspring/decimal
13+
//
14+
// # SEE ALSO
15+
// - examples/basic/floatop/rounding_error.go
16+
func UsingDecimalPkg() error {
17+
var (
18+
v = decimal.RequireFromString("0")
19+
)
20+
21+
for i := 0; i < 1000; i++ {
22+
v = v.Add(decimal.RequireFromString(".01"))
23+
}
24+
25+
output.Stdoutl("[result]", v.StringFixed(1))
26+
27+
return nil
28+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.21
55
require (
66
github.com/devlights/gomy v0.6.0
77
github.com/pelletier/go-toml/v2 v2.0.9
8+
github.com/shopspring/decimal v1.3.1
89
golang.org/x/crypto v0.11.0
910
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
1011
golang.org/x/sync v0.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N
77
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
88
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
99
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
11+
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
1012
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1113
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1214
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

0 commit comments

Comments
 (0)