numeric.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Author : Eric Kim
  2. // Build Date : 23 Jul 2018 Last Update 02 Aug 2018
  3. // End-Agent for Passcon Multi OS go binding with Windows, MacOS, iOS, and Android
  4. // All rights are reserved.
  5. package etc
  6. import (
  7. "fmt"
  8. "reflect"
  9. "strconv"
  10. )
  11. func DecimalToInt(decimal string) int {
  12. if tmp, err := strconv.ParseFloat(decimal, 4); err == nil {
  13. return int(tmp)
  14. } else {
  15. ErrLog("LJLJLJLJIUU-decimal unacceptable", err)
  16. return 999999999
  17. }
  18. }
  19. func DecimalToFloat64(decimal string) float64 {
  20. if num, err := strconv.ParseFloat(decimal, 4); err == nil {
  21. return num
  22. } else {
  23. ErrLog("UYTIUYKHKIU-decimal unacceptable", err)
  24. return 999999999999.9999
  25. }
  26. }
  27. func NumToDecimal(num interface{}, precision string) string {
  28. switch reflect.TypeOf(num).String() {
  29. case "int":
  30. tmp := float64(num.(int))
  31. return fmt.Sprintf("%"+precision+"f", tmp)
  32. case "int32":
  33. tmp := float64(num.(int))
  34. return fmt.Sprintf("%"+precision+"f", tmp)
  35. case "int64":
  36. tmp := float64(num.(int))
  37. return fmt.Sprintf("%"+precision+"f", tmp)
  38. case "float32":
  39. return fmt.Sprintf("%"+precision+"f", num)
  40. case "float64":
  41. return fmt.Sprintf("%"+precision+"f", num)
  42. default:
  43. return "Out of Range"
  44. }
  45. }
  46. func NumToStr(num interface{}) string {
  47. numType := reflect.TypeOf(num).String()
  48. switch reflect.TypeOf(num).String() {
  49. case "int":
  50. return fmt.Sprintf("%d", num)
  51. case "int32":
  52. return fmt.Sprintf("%d", num)
  53. case "int64":
  54. return fmt.Sprintf("%d", num)
  55. case "float32":
  56. return fmt.Sprintf("%f", num)
  57. case "float64":
  58. return fmt.Sprintf("%f", num)
  59. default:
  60. return numType + " is Out of Range"
  61. }
  62. }
  63. func StrToInt(num string) int {
  64. if i, err := strconv.Atoi(num); err == nil {
  65. return i
  66. } else {
  67. ErrLog("PIOUKHBJJCHYT-'Integer' unacceptable", err)
  68. return 0
  69. }
  70. }
  71. func StrToFloat(num string, decimalP int) float64 {
  72. if f, err := strconv.ParseFloat(num, decimalP); err == nil {
  73. return f
  74. } else {
  75. ErrLog("UYTIKHKYOUJ- 'Float' unacceptable", err)
  76. return 0
  77. }
  78. }