// Package mathutil provide math(int, number) util functions. eg: convert, math calc, random
package mathutilimport ()// Mul computes the `a*b` value, rounding the result.func [, comdef.Number]( , ) float64 {returnmath.Round(SafeFloat() * SafeFloat())}// MulF2i computes the float64 type a * b value, rounding the result to an integer.func (, float64) int {returnint(math.Round( * ))}// Div computes the `a/b` value, result uses a round handle.func [, comdef.Number]( , ) float64 {returnmath.Round(SafeFloat() / SafeFloat())}// DivInt computes the int type a / b value, rounding the result to an integer.func [ comdef.Integer](, ) int { := math.Round(float64() / float64())returnint()}// DivF2i computes the float64 type a / b value, rounding the result to an integer.func (, float64) int {returnint(math.Round( / ))}// Percent returns a value percentage of the total. eg: 1/100 = 1.0%func (, int) float64 {if == 0 {returnfloat64(0) }return (float64() / float64()) * 100}// Range a number range expression, and handle each value. eg: "1-100,123,124"func ( string, func( int)) error {for , := rangestrings.Split(, ",") { = strings.TrimSpace()if == "" {continue }// is range, eg: "1-100", "-20-2"if := checkfn.IndexByteAfter(, '-', 1); > 0 { , , := parseIntRange(, )if != nil {return }// range numberfor := ; <= ; ++ { () } } else { , := strconv.Atoi()if != nil {returnfmt.Errorf("invalid integer value: %q", ) } () } }returnnil}// Expand a number range expression to int[]. eg: "1-100,123,124"func ( string) ([]int, error) {var []intfor , := rangestrings.Split(, ",") {if == "" {continue }// is range, eg: "1-100", "-20-2"if := checkfn.IndexByteAfter(, '-', 1); > 0 { , := expandIntRange(, )if != nil {returnnil, } = append(, ...) } else { , := strconv.Atoi()if != nil {returnnil, fmt.Errorf("invalid integer value: %q", ) } = append(, ) } }return , nil}// 处理范围格式// eg: "1-30" -> [1, 30], "-20-2" -> [-20, 2]func parseIntRange( string, int) ( int, int, error) { , := [:], [+1:] , = strconv.Atoi()if != nil { = fmt.Errorf("invalid range start value: %q", )return } , = strconv.Atoi()if != nil { = fmt.Errorf("invalid range end value: %q", )return }// swap min and maxif > { , = , }return}// 将 "1-30" 转换为 int 列表func expandIntRange( string, int) ([]int, error) {var []int , , := parseIntRange(, )if != nil {returnnil, }for := ; <= ; ++ { = append(, ) }return , nil}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.