java中涉及到金钱的计算,都会用BigDecimal。而golang没有这个数据类型
    查阅资料发现,可以用golang官方包的大数计算代替:https://golang.org/src/math/big/example_test.go

    1. // Copyright 2012 The Go Authors. All rights reserved.
    2. // Use of this source code is governed by a BSD-style
    3. // license that can be found in the LICENSE file.
    4. package big_test
    5. import (
    6. "fmt"
    7. "log"
    8. "math"
    9. "math/big"
    10. )
    11. func ExampleRat_SetString() {
    12. r := new(big.Rat)
    13. r.SetString("355/113")
    14. fmt.Println(r.FloatString(3))
    15. // Output: 3.142
    16. }
    17. func ExampleInt_SetString() {
    18. i := new(big.Int)
    19. i.SetString("644", 8) // octal
    20. fmt.Println(i)
    21. // Output: 420
    22. }
    23. func ExampleRat_Scan() {
    24. // The Scan function is rarely used directly;
    25. // the fmt package recognizes it as an implementation of fmt.Scanner.
    26. r := new(big.Rat)
    27. _, err := fmt.Sscan("1.5000", r)
    28. if err != nil {
    29. log.Println("error scanning value:", err)
    30. } else {
    31. fmt.Println(r)
    32. }
    33. // Output: 3/2
    34. }
    35. func ExampleInt_Scan() {
    36. // The Scan function is rarely used directly;
    37. // the fmt package recognizes it as an implementation of fmt.Scanner.
    38. i := new(big.Int)
    39. _, err := fmt.Sscan("18446744073709551617", i)
    40. if err != nil {
    41. log.Println("error scanning value:", err)
    42. } else {
    43. fmt.Println(i)
    44. }
    45. // Output: 18446744073709551617
    46. }
    47. func ExampleFloat_Scan() {
    48. // The Scan function is rarely used directly;
    49. // the fmt package recognizes it as an implementation of fmt.Scanner.
    50. f := new(big.Float)
    51. _, err := fmt.Sscan("1.19282e99", f)
    52. if err != nil {
    53. log.Println("error scanning value:", err)
    54. } else {
    55. fmt.Println(f)
    56. }
    57. // Output: 1.19282e+99
    58. }
    59. // This example demonstrates how to use big.Int to compute the smallest
    60. // Fibonacci number with 100 decimal digits and to test whether it is prime.
    61. func Example_fibonacci() {
    62. // Initialize two big ints with the first two numbers in the sequence.
    63. a := big.NewInt(0)
    64. b := big.NewInt(1)
    65. // Initialize limit as 10^99, the smallest integer with 100 digits.
    66. var limit big.Int
    67. limit.Exp(big.NewInt(10), big.NewInt(99), nil)
    68. // Loop while a is smaller than 1e100.
    69. for a.Cmp(&limit) < 0 {
    70. // Compute the next Fibonacci number, storing it in a.
    71. a.Add(a, b)
    72. // Swap a and b so that b is the next number in the sequence.
    73. a, b = b, a
    74. }
    75. fmt.Println(a) // 100-digit Fibonacci number
    76. // Test a for primality.
    77. // (ProbablyPrimes' argument sets the number of Miller-Rabin
    78. // rounds to be performed. 20 is a good value.)
    79. fmt.Println(a.ProbablyPrime(20))
    80. // Output:
    81. // 1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757
    82. // false
    83. }
    84. // This example shows how to use big.Float to compute the square root of 2 with
    85. // a precision of 200 bits, and how to print the result as a decimal number.
    86. func Example_sqrt2() {
    87. // We'll do computations with 200 bits of precision in the mantissa.
    88. const prec = 200
    89. // Compute the square root of 2 using Newton's Method. We start with
    90. // an initial estimate for sqrt(2), and then iterate:
    91. // x_{n+1} = 1/2 * ( x_n + (2.0 / x_n) )
    92. // Since Newton's Method doubles the number of correct digits at each
    93. // iteration, we need at least log_2(prec) steps.
    94. steps := int(math.Log2(prec))
    95. // Initialize values we need for the computation.
    96. two := new(big.Float).SetPrec(prec).SetInt64(2)
    97. half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
    98. // Use 1 as the initial estimate.
    99. x := new(big.Float).SetPrec(prec).SetInt64(1)
    100. // We use t as a temporary variable. There's no need to set its precision
    101. // since big.Float values with unset (== 0) precision automatically assume
    102. // the largest precision of the arguments when used as the result (receiver)
    103. // of a big.Float operation.
    104. t := new(big.Float)
    105. // Iterate.
    106. for i := 0; i <= steps; i++ {
    107. t.Quo(two, x) // t = 2.0 / x_n
    108. t.Add(x, t) // t = x_n + (2.0 / x_n)
    109. x.Mul(half, t) // x_{n+1} = 0.5 * t
    110. }
    111. // We can use the usual fmt.Printf verbs since big.Float implements fmt.Formatter
    112. fmt.Printf("sqrt(2) = %.50f\n", x)
    113. // Print the error between 2 and x*x.
    114. t.Mul(x, x) // t = x*x
    115. fmt.Printf("error = %e\n", t.Sub(two, t))
    116. // Output:
    117. // sqrt(2) = 1.41421356237309504880168872420969807856967187537695
    118. // error = 0.000000e+00
    119. }