github.com/aldelo/common@v1.5.1/helper-num.go (about)

     1  package helper
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  /*
    11   * Copyright 2020-2023 Aldelo, LP
    12   *
    13   * Licensed under the Apache License, Version 2.0 (the "License");
    14   * you may not use this file except in compliance with the License.
    15   * You may obtain a copy of the License at
    16   *
    17   *     http://www.apache.org/licenses/LICENSE-2.0
    18   *
    19   * Unless required by applicable law or agreed to in writing, software
    20   * distributed under the License is distributed on an "AS IS" BASIS,
    21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    22   * See the License for the specific language governing permissions and
    23   * limitations under the License.
    24   */
    25  
    26  // AbsInt returns absolute value of i
    27  func AbsInt(i int) int {
    28  	if i < 0 {
    29  		return i * -1
    30  	} else {
    31  		return i
    32  	}
    33  }
    34  
    35  // AbsInt64 returns absolute value of i
    36  func AbsInt64(i int64) int64 {
    37  	if i < 0 {
    38  		return i * -1
    39  	} else {
    40  		return i
    41  	}
    42  }
    43  
    44  // AbsDuration returns absolute value of d
    45  func AbsDuration(d time.Duration) time.Duration {
    46  	if d < 0 {
    47  		return d * -1
    48  	} else {
    49  		return d
    50  	}
    51  }
    52  
    53  // AbsFloat64 returns absolute value of f
    54  func AbsFloat64(f float64) float64 {
    55  	if f < 0 {
    56  		return f * -1
    57  	} else {
    58  		return f
    59  	}
    60  }
    61  
    62  // IsInt32 tests if input string is integer (whole numbers 32 bits)
    63  func IsInt32(s string) bool {
    64  	if _, err := strconv.ParseInt(strings.TrimSpace(s), 10, 32); err != nil {
    65  
    66  		return false
    67  	}
    68  
    69  	return true
    70  }
    71  
    72  // IsInt64 tests if input string is big integer (whole number greater 64 bits)
    73  func IsInt64(s string) bool {
    74  	if _, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64); err != nil {
    75  		return false
    76  	}
    77  
    78  	return true
    79  }
    80  
    81  // IsFloat32 tests if input string is float 32 bit (decimal point value)
    82  func IsFloat32(s string) bool {
    83  	if _, err := strconv.ParseFloat(strings.TrimSpace(s), 32); err != nil {
    84  		return false
    85  	}
    86  
    87  	return true
    88  }
    89  
    90  // IsFloat64 tests if input string is float 64 bit (decimal point value)
    91  func IsFloat64(s string) bool {
    92  	if _, err := strconv.ParseFloat(strings.TrimSpace(s), 64); err != nil {
    93  		return false
    94  	}
    95  
    96  	return true
    97  }
    98  
    99  // IsBoolType tests if input string is boolean
   100  func IsBoolType(s string) bool {
   101  	x := []string{"yes", "on", "running", "started"}
   102  
   103  	if StringSliceContains(&x, strings.ToLower(s)) {
   104  		s = "true"
   105  	}
   106  
   107  	if _, err := strconv.ParseBool(s); err != nil {
   108  		return false
   109  	}
   110  
   111  	return true
   112  }
   113  
   114  // ParseInt32 tests and parses if input string is integer (whole numbers 32 bits)
   115  func ParseInt32(s string) (int, bool) {
   116  	if strings.Index(s, ".") >= 0 {
   117  		s = SplitString(s, ".", 0)
   118  	}
   119  
   120  	var result int64
   121  	var err error
   122  
   123  	if result, err = strconv.ParseInt(strings.TrimSpace(s), 10, 32); err != nil {
   124  		return 0, false
   125  	}
   126  
   127  	if result >= math.MinInt && result <= math.MaxInt {
   128  		return int(result), true
   129  	} else {
   130  		return 0, false
   131  	}
   132  }
   133  
   134  // ParseInt64 tests and parses if input string is big integer (whole number greater 64 bits)
   135  func ParseInt64(s string) (int64, bool) {
   136  	if strings.Index(s, ".") >= 0 {
   137  		s = SplitString(s, ".", 0)
   138  	}
   139  
   140  	var result int64
   141  	var err error
   142  
   143  	if result, err = strconv.ParseInt(strings.TrimSpace(s), 10, 64); err != nil {
   144  		return 0, false
   145  	}
   146  
   147  	return result, true
   148  }
   149  
   150  // ParseFloat32 tests and parses if input string is float 32 bit (decimal point value)
   151  func ParseFloat32(s string) (float32, bool) {
   152  	var result float64
   153  	var err error
   154  
   155  	if result, err = strconv.ParseFloat(strings.TrimSpace(s), 32); err != nil {
   156  		return 0.00, false
   157  	}
   158  
   159  	return float32(result), true
   160  }
   161  
   162  // ParseFloat64 tests and parses if input string is float 64 bit (decimal point value)
   163  func ParseFloat64(s string) (float64, bool) {
   164  	var result float64
   165  	var err error
   166  
   167  	if result, err = strconv.ParseFloat(strings.TrimSpace(s), 64); err != nil {
   168  		return 0.00, false
   169  	}
   170  
   171  	return result, true
   172  }
   173  
   174  // ParseBool tests and parses if input string is boolean,
   175  // return value 1st bool is the boolean result,
   176  // return value 2nd bool is the ParseBool success or failure indicator
   177  func ParseBool(s string) (bool, bool) {
   178  	var result bool
   179  	var err error
   180  
   181  	x := []string{"yes", "on", "running", "started", "y", "1"}
   182  
   183  	if StringSliceContains(&x, strings.ToLower(s)) {
   184  		s = "true"
   185  	}
   186  
   187  	if result, err = strconv.ParseBool(s); err != nil {
   188  		return false, false
   189  	}
   190  
   191  	return result, true
   192  }
   193  
   194  // ExponentialToNumber converts exponential representation of a number into actual number equivalent
   195  func ExponentialToNumber(exp string) string {
   196  	if strings.Index(strings.ToLower(exp), "e") >= 0 {
   197  		v, _ := ParseFloat64(exp)
   198  		return strconv.FormatFloat(v, 'f', -1, 64)
   199  	} else {
   200  		return exp
   201  	}
   202  }
   203  
   204  // RoundFloat64 converts float64 value to target precision
   205  func RoundFloat64(val float64, precision uint) float64 {
   206  	ratio := math.Pow(10, float64(precision))
   207  	return math.Round(val*ratio) / ratio
   208  }
   209  
   210  // RoundIntegerToNearestBlock returns conformed block size value where val fall into.
   211  // For example: if val is 3 and blockSize is 5, then return value is 5;
   212  // where as if val is 13 and blockSize is 5, then return value is 15;
   213  // val must be > 0
   214  func RoundIntegerToNearestBlock(val int, blockSize int) int {
   215  	if val < 0 {
   216  		return -1
   217  	}
   218  
   219  	if blockSize < 1 {
   220  		return -1
   221  	}
   222  
   223  	if val == 0 {
   224  		return blockSize
   225  	}
   226  
   227  	number := float64(val) / float64(blockSize)
   228  	whole := RoundFloat64(number, 0)
   229  	frac := number - whole
   230  
   231  	if frac <= 0 {
   232  		return int(whole) * blockSize
   233  	} else {
   234  		return (int(whole) + 1) * blockSize
   235  	}
   236  }