numberutil

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package numberutil numberutil 包用于封装数值类型的辅助函数

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualFloat32Prec

func EqualFloat32Prec(x, y float32, decimals int) bool

EqualFloat32Prec 在给定精度范围内比较两个 float32 是否相等 decimals 表示小数点后精度的位数 note: 效率比 EqualFloat64 差

func EqualFloat64

func EqualFloat64(x, y, limit float64) bool

EqualFloat64 在给定精度范围内比较两个 float64 是否相等 if limit < 0,则精度设置为机器所能达到的最大精度 if x 和 y 的两个近似值的差小于limit,则判定x,y是相等的

Example
type args struct {
	x float64
	y float64
}

data := []args{
	{0.2, 0.2},
}

for _, v := range data {
	fmt.Printf("%-5t, %-5t, %-5t, ", v.x == v.y, EqualFloat64(v.x, v.y, -1), EqualFloat64(v.x, v.y, 0.000000000001))
	fmt.Println(v.x, v.y)
}

// 输出结果如下:
// true  true  true  0.2 0.2
// false false false 0.6 0.61
// false false true  0.799 0.8

func EqualFloat64Prec

func EqualFloat64Prec(x, y float64, decimals int) bool

EqualFloat64Prec 在给定精度范围内比较两个 float64 是否相等 decimals 表示小数点后精度的位数 note: 效率比 EqualFloat64 差

func IntFromFloat64

func IntFromFloat64(x float64) (int, error)

func NumberStr

func NumberStr(n int64, sep rune) string

NumberStr 数字千分位表示。数字转换成字符串,每 3 位使用 sep 分隔 NumberStr(10000, ',') 输出: 10,000

func Round

func Round(x float64, n int) float64

Round 对 x 四舍五入保留 n 为小数点 note: 若 x 正好是整数或小数点后位数不够n位,不进行补齐; 栗子(n=3,保留 3 位小数点):1.0 -> 1.0; 1 -> 1; 1.1116 -> 1.112

func Uint8FromInt

func Uint8FromInt(x int) (uint8, error)

Uint8FromInt int 转 uint8

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL