File tree Expand file tree Collapse file tree 5 files changed +33
-0
lines changed Expand file tree Collapse file tree 5 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 6
6
| ----| ------------| ----|
7
7
| order\_ of\_ computation.go| floatop\_ order\_ of\_ computation| 浮動小数点は計算の順序によって結果が変わることのサンプルです|
8
8
| rounding\_ error.go| floatop\_ rounding\_ error| 小数点計算において近似値が利用され丸め誤差が出るサンプルです|
9
+ | using\_ decimal\_ pkg.go| floatop\_ using\_ decimal\_ pkg| 小数点計算を github.com/shopspring/decimal パッケージを利用して処理するサンプルです|
Original file line number Diff line number Diff line change @@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
15
15
func (r * register ) Regist (m mapping.ExampleMapping ) {
16
16
m ["floatop_order_of_computation" ] = OrderOfComputation
17
17
m ["floatop_rounding_error" ] = RoundingError
18
+ m ["floatop_using_decimal_pkg" ] = UsingDecimalPkg
18
19
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 5
5
require (
6
6
github.com/devlights/gomy v0.6.0
7
7
github.com/pelletier/go-toml/v2 v2.0.9
8
+ github.com/shopspring/decimal v1.3.1
8
9
golang.org/x/crypto v0.11.0
9
10
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
10
11
golang.org/x/sync v0.3.0
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N
7
7
github.com/pelletier/go-toml/v2 v2.0.9 /go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc =
8
8
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
9
9
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 =
10
12
github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
11
13
github.com/stretchr/objx v0.4.0 /go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw =
12
14
github.com/stretchr/objx v0.5.0 /go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo =
You can’t perform that action at this time.
0 commit comments