You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

63 lines
1.6 KiB

  1. package inf_test
  2. import (
  3. "fmt"
  4. "log"
  5. )
  6. import "gopkg.in/inf.v0"
  7. func ExampleDec_SetString() {
  8. d := new(inf.Dec)
  9. d.SetString("012345.67890") // decimal; leading 0 ignored; trailing 0 kept
  10. fmt.Println(d)
  11. // Output: 12345.67890
  12. }
  13. func ExampleDec_Scan() {
  14. // The Scan function is rarely used directly;
  15. // the fmt package recognizes it as an implementation of fmt.Scanner.
  16. d := new(inf.Dec)
  17. _, err := fmt.Sscan("184467440.73709551617", d)
  18. if err != nil {
  19. log.Println("error scanning value:", err)
  20. } else {
  21. fmt.Println(d)
  22. }
  23. // Output: 184467440.73709551617
  24. }
  25. func ExampleDec_QuoRound_scale2RoundDown() {
  26. // 10 / 3 is an infinite decimal; it has no exact Dec representation
  27. x, y := inf.NewDec(10, 0), inf.NewDec(3, 0)
  28. // use 2 digits beyond the decimal point, round towards 0
  29. z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundDown)
  30. fmt.Println(z)
  31. // Output: 3.33
  32. }
  33. func ExampleDec_QuoRound_scale2RoundCeil() {
  34. // -42 / 400 is an finite decimal with 3 digits beyond the decimal point
  35. x, y := inf.NewDec(-42, 0), inf.NewDec(400, 0)
  36. // use 2 digits beyond decimal point, round towards positive infinity
  37. z := new(inf.Dec).QuoRound(x, y, 2, inf.RoundCeil)
  38. fmt.Println(z)
  39. // Output: -0.10
  40. }
  41. func ExampleDec_QuoExact_ok() {
  42. // 1 / 25 is a finite decimal; it has exact Dec representation
  43. x, y := inf.NewDec(1, 0), inf.NewDec(25, 0)
  44. z := new(inf.Dec).QuoExact(x, y)
  45. fmt.Println(z)
  46. // Output: 0.04
  47. }
  48. func ExampleDec_QuoExact_fail() {
  49. // 1 / 3 is an infinite decimal; it has no exact Dec representation
  50. x, y := inf.NewDec(1, 0), inf.NewDec(3, 0)
  51. z := new(inf.Dec).QuoExact(x, y)
  52. fmt.Println(z)
  53. // Output: <nil>
  54. }