本站源代码
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.

98 lines
2.7KB

  1. package govalidator
  2. import (
  3. "math"
  4. "reflect"
  5. )
  6. // Abs returns absolute value of number
  7. func Abs(value float64) float64 {
  8. return math.Abs(value)
  9. }
  10. // Sign returns signum of number: 1 in case of value > 0, -1 in case of value < 0, 0 otherwise
  11. func Sign(value float64) float64 {
  12. if value > 0 {
  13. return 1
  14. } else if value < 0 {
  15. return -1
  16. } else {
  17. return 0
  18. }
  19. }
  20. // IsNegative returns true if value < 0
  21. func IsNegative(value float64) bool {
  22. return value < 0
  23. }
  24. // IsPositive returns true if value > 0
  25. func IsPositive(value float64) bool {
  26. return value > 0
  27. }
  28. // IsNonNegative returns true if value >= 0
  29. func IsNonNegative(value float64) bool {
  30. return value >= 0
  31. }
  32. // IsNonPositive returns true if value <= 0
  33. func IsNonPositive(value float64) bool {
  34. return value <= 0
  35. }
  36. // InRange returns true if value lies between left and right border
  37. func InRangeInt(value, left, right interface{}) bool {
  38. value64, _ := ToInt(value)
  39. left64, _ := ToInt(left)
  40. right64, _ := ToInt(right)
  41. if left64 > right64 {
  42. left64, right64 = right64, left64
  43. }
  44. return value64 >= left64 && value64 <= right64
  45. }
  46. // InRange returns true if value lies between left and right border
  47. func InRangeFloat32(value, left, right float32) bool {
  48. if left > right {
  49. left, right = right, left
  50. }
  51. return value >= left && value <= right
  52. }
  53. // InRange returns true if value lies between left and right border
  54. func InRangeFloat64(value, left, right float64) bool {
  55. if left > right {
  56. left, right = right, left
  57. }
  58. return value >= left && value <= right
  59. }
  60. // InRange returns true if value lies between left and right border, generic type to handle int, float32 or float64, all types must the same type
  61. func InRange(value interface{}, left interface{}, right interface{}) bool {
  62. reflectValue := reflect.TypeOf(value).Kind()
  63. reflectLeft := reflect.TypeOf(left).Kind()
  64. reflectRight := reflect.TypeOf(right).Kind()
  65. if reflectValue == reflect.Int && reflectLeft == reflect.Int && reflectRight == reflect.Int {
  66. return InRangeInt(value.(int), left.(int), right.(int))
  67. } else if reflectValue == reflect.Float32 && reflectLeft == reflect.Float32 && reflectRight == reflect.Float32 {
  68. return InRangeFloat32(value.(float32), left.(float32), right.(float32))
  69. } else if reflectValue == reflect.Float64 && reflectLeft == reflect.Float64 && reflectRight == reflect.Float64 {
  70. return InRangeFloat64(value.(float64), left.(float64), right.(float64))
  71. } else {
  72. return false
  73. }
  74. }
  75. // IsWhole returns true if value is whole number
  76. func IsWhole(value float64) bool {
  77. return math.Remainder(value, 1) == 0
  78. }
  79. // IsNatural returns true if value is natural number (positive and whole)
  80. func IsNatural(value float64) bool {
  81. return IsWhole(value) && IsPositive(value)
  82. }
上海开阖软件有限公司 沪ICP备12045867号-1