floatKit

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package floatKit

浮点数的高精度运算.

Package floatKit

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(delta float64, ps ...float64) float64

Add 加

func Ceil

func Ceil(f float64, places int32) float64

Ceil 向上取整,类似于 math.Ceil(),但功能更强大

PS: 个人感觉: x轴向右.

e.g. (3.14, 1) => 3.2 (-3.14, 1) => -3.1

func Div

func Div(delta float64, ps ...float64) float64

Div 除法(不额外处理小数位)

func DivRound

func DivRound(precision int32, delta float64, ps ...float64) float64

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

func Floor(f float64, places int32) float64

Floor 向下取整,类似于 math.Floor(),但功能更强大

PS: (1) 个人感觉: x轴向左.

e.g. (3.14, 1) => 3.1 (-3.14, 1) => -3.2

func FormatFloat32ToString

func FormatFloat32ToString(f float32, fmt byte, prec int) string

FormatFloat32ToString 类型转换: float32 => string

func FormatFloat64ToString

func FormatFloat64ToString(f float64, fmt byte, prec int) string

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 Mul

func Mul(delta float64, ps ...float64) float64

Mul 乘

func ParseStringToFloat32

func ParseStringToFloat32(str string) (float32, error)

ParseStringToFloat32 类型转换: string => float32

e.g. ("3.141592653589793") => 3.1415927, nil ("") => 0, strconv.ParseFloat: parsing "": invalid syntax

func ParseStringToFloat32WithDefault

func ParseStringToFloat32WithDefault(str string, def float32) float32

func ParseStringToFloat64

func ParseStringToFloat64(str string) (float64, error)

ParseStringToFloat64 类型转换: string => float64

e.g. ("3.141592653589793") => (3.141592653589793, nil) ("") => 0, strconv.ParseFloat: parsing "": invalid syntax

func ParseStringToFloat64WithDefault

func ParseStringToFloat64WithDefault(str string, def float64) float64

func Round

func Round(f float64, places int32) float64

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 Sub

func Sub(delta float64, ps ...float64) float64

Sub 减

func ToFloat32

func ToFloat32(i interface{}) float32

func ToFloat32E added in v2.0.10

func ToFloat32E(i interface{}) (float32, error)

func ToFloat64

func ToFloat64(i interface{}) float64

func ToFloat64E added in v2.0.10

func ToFloat64E(i interface{}) (float64, error)

func ToReadableString added in v2.0.9

func ToReadableString(f float64) string

ToReadableString 去除后面的无意义的"0"

PS: (1) 十进制; (2) 去掉后面的无意义的"0"; (3) 同类的方法: humanize.Ftoa().

e.g. (2.24) => "2.24" (2.0000) => "2" (2.000010000) => "2.00001"

Types

type Float32

type Float32 struct {
	// contains filtered or unexported fields
}

func NewFloat32

func NewFloat32(value float32) *Float32

func (*Float32) GetValue

func (c *Float32) GetValue() float32

type Float64

type Float64 struct {
	// contains filtered or unexported fields
}

func NewFloat64

func NewFloat64(value float64) *Float64

func (*Float64) GetValue

func (c *Float64) GetValue() float64

Jump to

Keyboard shortcuts

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