Documentation ¶
Overview ¶
Package floatKit
浮点数的高精度运算.
Package floatKit
Index ¶
- func Add(delta float64, ps ...float64) float64
- func Ceil(f float64, places int32) float64
- func Div(delta float64, ps ...float64) float64
- func DivRound(precision int32, delta float64, ps ...float64) float64
- func Floor(f float64, places int32) float64
- func FormatFloat32ToString(f float32, fmt byte, prec int) string
- func FormatFloat64ToString(f float64, fmt byte, prec int) string
- func Mul(delta float64, ps ...float64) float64
- func ParseStringToFloat32(str string) (float32, error)
- func ParseStringToFloat32WithDefault(str string, def float32) float32
- func ParseStringToFloat64(str string) (float64, error)
- func ParseStringToFloat64WithDefault(str string, def float64) float64
- func Round(f float64, places int32) float64
- func Sub(delta float64, ps ...float64) float64
- func ToFloat32(i interface{}) float32
- func ToFloat32E(i interface{}) (float32, error)
- func ToFloat64(i interface{}) float64
- func ToFloat64E(i interface{}) (float64, error)
- func ToReadableString(f float64) string
- type Float32
- type Float64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ceil ¶
Ceil 向上取整,类似于 math.Ceil(),但功能更强大
PS: 个人感觉: x轴向右.
e.g. (3.14, 1) => 3.2 (-3.14, 1) => -3.1
func DivRound ¶
DivRound 除法(四舍五入),divides and rounds to a given precision
@param precision 小数位数
e.g. (4, 1, 2) => 0.5 (4, 1, 3) => 0.3333 (4, 0.123456) => 0.1235
func Floor ¶
Floor 向下取整,类似于 math.Floor(),但功能更强大
PS: 个人感觉: x轴向左.
e.g. (3.14, 1) => 3.1 (-3.14, 1) => -3.2
func FormatFloat32ToString ¶
FormatFloat32ToString 类型转换: float32 => string
func FormatFloat64ToString ¶
FormatFloat64ToString 类型转换: float64 => string
@param fmt (1) "f": -ddd.dddd(十进制)
(2) "b": -ddddp±ddd,指数为二进制 (3) "e": -d.dddde±dd,十进制指数 (4) "E": -d.ddddE±dd,十进制指数 (5) "g": 指数很大时用"e"格式,否则"f"格式 (6) "G": 指数很大时用"E"格式,否则"f"格式
@param prec (1) 如果传参fmt为"f"、"e"、"E",它表示小数点后的数字个数
(2) 如果传参fmt为"g"、"G",它控制总的数字个数 (3) -1: 使用"最少数量但又必需"的数字来表示传参f
func ParseStringToFloat32 ¶
ParseStringToFloat32 类型转换: string => float32
e.g. ("3.141592653589793") => 3.1415927, nil ("") => 0, strconv.ParseFloat: parsing "": invalid syntax
func ParseStringToFloat64 ¶
ParseStringToFloat64 类型转换: string => float64
e.g. ("3.141592653589793") => (3.141592653589793, nil) ("") => 0, strconv.ParseFloat: parsing "": invalid syntax
func Round ¶
Round 保留小数位(四舍五入),类似于 math.Round(),但功能更强大
PS: (1) 参考:https://zhuanlan.zhihu.com/p/152050239?from_voters_page=true (2) 个人感觉:先把正负号拿出来 => 进行取舍 => 把正负号还回去 (3) 传参为负数的情况下,Golang的四舍五入与别的语言(Java、JavaScript)不同,详情见"Golang.docx"中的"math标准库".
@param places 小数位数(如果最后几个都是0的话,会省略掉);可以为负值
e.g. (3.14, 1) => 3.1 (3.15, 1) => 3.2 (-3.14, 1) => -3.1 (-3.15, 1) => -3.2
(3.1001, 2) => 3.1 (521, -1) => 520
func ToFloat32E ¶ added in v2.0.10
func ToFloat64E ¶ added in v2.0.10
func ToReadableString ¶ added in v2.0.9
ToReadableString 去除后面的无意义的"0"
PS: (1) 十进制; (2) 去掉后面的无意义的"0"; (3) 同类的方法: humanize.Ftoa().
e.g. (2.24) => "2.24" (2.0000) => "2" (2.000010000) => "2.00001"