decimal_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. "testing"
  6. "github.com/shopspring/decimal"
  7. )
  8. func Test111(t *testing.T) {
  9. amount, _ := decimal.NewFromString("327312.6934")
  10. amountFlo := amount.IntPart()
  11. t.Log(amountFlo)
  12. count := int(amountFlo)/100000 + 1
  13. total := 0
  14. for i := 1; i < count; i++ {
  15. total = total + 100000
  16. }
  17. t.Log(total)
  18. }
  19. func Test222(t *testing.T) {
  20. amount, _ := decimal.NewFromString("300000.2934")
  21. invoicePart := int(math.Ceil(amount.InexactFloat64() / 100000))
  22. t.Log(invoicePart)
  23. }
  24. func Test333(t *testing.T) {
  25. amount, _ := decimal.NewFromString("-290.83")
  26. t.Log(amount)
  27. t.Log(amount.Neg())
  28. amount2, _ := decimal.NewFromString("290.83")
  29. t.Log(amount2)
  30. t.Log(amount2.Neg())
  31. }
  32. func Test444(t *testing.T) {
  33. amount, _ := decimal.NewFromString("300000.2934")
  34. t.Log(amount.RoundDown(2))
  35. t.Log(amount.RoundDown(2).StringFixed(2))
  36. amount1, _ := decimal.NewFromString("300000.2")
  37. t.Log(amount1.RoundDown(2))
  38. t.Log(amount1.RoundDown(2).StringFixed(2))
  39. amount2, _ := decimal.NewFromString("300000.297")
  40. t.Log(amount2.RoundDown(2))
  41. t.Log(amount2.RoundDown(2).StringFixed(2))
  42. amount3, _ := decimal.NewFromString("300000")
  43. t.Log(amount3.RoundDown(2))
  44. t.Log(amount3.RoundDown(2).StringFixed(2))
  45. amount4, _ := decimal.NewFromString("300000.2834")
  46. t.Log(amount4.RoundFloor(2))
  47. t.Log(amount4.RoundDown(2))
  48. t.Log(amount4.RoundUp(2))
  49. t.Log(amount4.RoundBank(2))
  50. t.Log(amount4.RoundCeil(2))
  51. }
  52. func Test555(t *testing.T) {
  53. amount1, _ := decimal.NewFromString("300000.2834")
  54. max := decimal.NewFromFloat(100000)
  55. t.Log(amount1.Div(max).RoundUp(0).IntPart())
  56. amount2, _ := decimal.NewFromString("210000.2834")
  57. t.Log(amount2.Div(max).RoundUp(0).IntPart())
  58. amount3, _ := decimal.NewFromString("200000")
  59. t.Log(amount3.Div(max).RoundUp(0).IntPart())
  60. }
  61. func Test666(t *testing.T) {
  62. t.Log(decimal.NewFromInt(30).Mul(decimal.NewFromFloat(0.01)).StringFixed(2))
  63. }
  64. func Test_New(t *testing.T) {
  65. amount1, _ := decimal.NewFromString("300012.2834")
  66. t.Log(amount1)
  67. t.Log(amount1.String())
  68. t.Log(amount1.IntPart()) //获取整数部分
  69. t.Log(amount1.InexactFloat64())
  70. fmt.Println()
  71. amount2 := decimal.NewFromFloat(300012.2834)
  72. t.Log(amount2)
  73. t.Log(amount2.String())
  74. fmt.Println()
  75. amount3 := decimal.NewFromInt(300012)
  76. t.Log(amount3)
  77. t.Log(amount3.String())
  78. t.Log(amount3.InexactFloat64())
  79. }
  80. func Test_StringFixed(t *testing.T) {
  81. amount1, _ := decimal.NewFromString("300012.2834")
  82. t.Log(amount1.StringFixed(4)) // 300012.2834
  83. amount2, _ := decimal.NewFromString("300012.2834")
  84. t.Log(amount2.StringFixed(3)) // 300012.283
  85. amount3, _ := decimal.NewFromString("300012.2834")
  86. t.Log(amount3.StringFixed(2)) // 300012.28
  87. amount4, _ := decimal.NewFromString("300012.2834")
  88. t.Log(amount4.StringFixed(1)) // 300012.3
  89. amount5, _ := decimal.NewFromString("300012.2834")
  90. t.Log(amount5.StringFixed(0)) // 300012
  91. amount6, _ := decimal.NewFromString("300012.2834")
  92. t.Log(amount6.StringFixed(-1)) // 300010
  93. }
  94. func Test_Round(t *testing.T) {
  95. amount1, _ := decimal.NewFromString("300000.2834")
  96. t.Log(amount1.Round(2).StringFixed(2)) // 300000.28
  97. amount2, _ := decimal.NewFromString("300000.2864")
  98. t.Log(amount2.Round(2).StringFixed(2)) // 00000.29
  99. amount3, _ := decimal.NewFromString("300000.2")
  100. t.Log(amount3.Round(2).StringFixed(2)) // 300000.20
  101. amount4, _ := decimal.NewFromString("300000")
  102. t.Log(amount4.Round(2).StringFixed(2)) // 300000.00
  103. t.Log("--- StringFixed ---")
  104. amount5, _ := decimal.NewFromString("300000.2834")
  105. t.Log(amount5.StringFixed(2)) // 300000.28
  106. amount6, _ := decimal.NewFromString("300000.2864")
  107. t.Log(amount6.StringFixed(2)) // 300000.29
  108. amount7, _ := decimal.NewFromString("300000.2")
  109. t.Log(amount7.StringFixed(2)) // 300000.20
  110. amount8, _ := decimal.NewFromString("300000")
  111. t.Log(amount8.StringFixed(2)) // 300000.00
  112. t.Log("--- Round ---")
  113. amount9, _ := decimal.NewFromString("300000.2834")
  114. t.Log(amount9.Round(2)) // 300000.28
  115. amount10, _ := decimal.NewFromString("300000.2864")
  116. t.Log(amount10.Round(2)) // 00000.29
  117. amount11, _ := decimal.NewFromString("300000.2")
  118. t.Log(amount11.Round(2)) // 300000.2
  119. amount12, _ := decimal.NewFromString("300000")
  120. t.Log(amount12.Round(2)) // 300000
  121. }
  122. func Test_RoundUp(t *testing.T) {
  123. amount1, _ := decimal.NewFromString("300000.2834")
  124. t.Log(amount1.RoundUp(2).StringFixed(2)) // 300000.29
  125. amount2, _ := decimal.NewFromString("300000.2864")
  126. t.Log(amount2.RoundUp(2).StringFixed(2)) // 300000.29
  127. amount3, _ := decimal.NewFromString("300000.2")
  128. t.Log(amount3.RoundUp(2).StringFixed(2)) // 300000.20
  129. amount4, _ := decimal.NewFromString("300000")
  130. t.Log(amount4.RoundUp(2).StringFixed(2)) //300000.00
  131. }
  132. func Test_RoundDown(t *testing.T) {
  133. amount1, _ := decimal.NewFromString("300000.2834")
  134. t.Log(amount1.RoundDown(2).StringFixed(2)) //300000.28
  135. amount2, _ := decimal.NewFromString("300000.2864")
  136. t.Log(amount2.RoundDown(2).StringFixed(2)) //300000.28
  137. amount3, _ := decimal.NewFromString("300000.2")
  138. t.Log(amount3.RoundDown(2).StringFixed(2)) //300000.20
  139. amount4, _ := decimal.NewFromString("300000")
  140. t.Log(amount4.StringFixed(2)) // 300000.20
  141. }