gconv_int.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gconv
  7. import (
  8. "math"
  9. "strconv"
  10. "github.com/gogf/gf/v2/encoding/gbinary"
  11. )
  12. // Int converts `any` to int.
  13. func Int(any interface{}) int {
  14. if any == nil {
  15. return 0
  16. }
  17. if v, ok := any.(int); ok {
  18. return v
  19. }
  20. return int(Int64(any))
  21. }
  22. // Int8 converts `any` to int8.
  23. func Int8(any interface{}) int8 {
  24. if any == nil {
  25. return 0
  26. }
  27. if v, ok := any.(int8); ok {
  28. return v
  29. }
  30. return int8(Int64(any))
  31. }
  32. // Int16 converts `any` to int16.
  33. func Int16(any interface{}) int16 {
  34. if any == nil {
  35. return 0
  36. }
  37. if v, ok := any.(int16); ok {
  38. return v
  39. }
  40. return int16(Int64(any))
  41. }
  42. // Int32 converts `any` to int32.
  43. func Int32(any interface{}) int32 {
  44. if any == nil {
  45. return 0
  46. }
  47. if v, ok := any.(int32); ok {
  48. return v
  49. }
  50. return int32(Int64(any))
  51. }
  52. // Int64 converts `any` to int64.
  53. func Int64(any interface{}) int64 {
  54. if any == nil {
  55. return 0
  56. }
  57. switch value := any.(type) {
  58. case int:
  59. return int64(value)
  60. case int8:
  61. return int64(value)
  62. case int16:
  63. return int64(value)
  64. case int32:
  65. return int64(value)
  66. case int64:
  67. return value
  68. case uint:
  69. return int64(value)
  70. case uint8:
  71. return int64(value)
  72. case uint16:
  73. return int64(value)
  74. case uint32:
  75. return int64(value)
  76. case uint64:
  77. return int64(value)
  78. case float32:
  79. return int64(value)
  80. case float64:
  81. return int64(value)
  82. case bool:
  83. if value {
  84. return 1
  85. }
  86. return 0
  87. case []byte:
  88. return gbinary.DecodeToInt64(value)
  89. default:
  90. if f, ok := value.(iInt64); ok {
  91. return f.Int64()
  92. }
  93. var (
  94. s = String(value)
  95. isMinus = false
  96. )
  97. if len(s) > 0 {
  98. if s[0] == '-' {
  99. isMinus = true
  100. s = s[1:]
  101. } else if s[0] == '+' {
  102. s = s[1:]
  103. }
  104. }
  105. // Hexadecimal
  106. if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
  107. if v, e := strconv.ParseInt(s[2:], 16, 64); e == nil {
  108. if isMinus {
  109. return -v
  110. }
  111. return v
  112. }
  113. }
  114. // Decimal
  115. if v, e := strconv.ParseInt(s, 10, 64); e == nil {
  116. if isMinus {
  117. return -v
  118. }
  119. return v
  120. }
  121. // Float64
  122. if valueInt64 := Float64(value); math.IsNaN(valueInt64) {
  123. return 0
  124. } else {
  125. return int64(valueInt64)
  126. }
  127. }
  128. }