Comments (4)
浮点数运算基于 IEEE754 标准,丢精度可能是无法避免的
我看别人的代码有种精度更高的实现方式:
func Round(val float64, precision int) float64 {
if precision == 0 {
return math.Round(val)
}
p := math.Pow10(precision)
if precision < 0 {
return math.Floor(val*p+0.5) * math.Pow10(-precision)
}
return math.Floor(val*p+0.5) / p
}
但是仍然无法避免这个问题,如果需要准确的运算,你或许可以试试 github.com/shopspring/decimal
from lancet.
可以使用decimal包解决
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string {
d, _ := decimal.NewFromString(convertor.ToString(x))
f, _ := d.Round(int32(n)).Float64()
return strconv.FormatFloat(f, 'f', n, 64)
}
浮点数运算基于 IEEE754 标准,丢精度可能是无法避免的
我看别人的代码有种精度更高的实现方式:
func Round(val float64, precision int) float64 { if precision == 0 { return math.Round(val) } p := math.Pow10(precision) if precision < 0 { return math.Floor(val*p+0.5) * math.Pow10(-precision) } return math.Floor(val*p+0.5) / p }但是仍然无法避免这个问题,如果需要准确的运算,你或许可以试试 github.com/shopspring/decimal
from lancet.
https://github.com/duke-git/lancet/blob/main/README_zh-CN.md?plain=1#L27
项目介绍里提到 只依赖 go 标准库和 golang.org/x
可以使用decimal包解决
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string { d, _ := decimal.NewFromString(convertor.ToString(x)) f, _ := d.Round(int32(n)).Float64() return strconv.FormatFloat(f, 'f', n, 64) }
浮点数运算基于 IEEE754 标准,丢精度可能是无法避免的
我看别人的代码有种精度更高的实现方式:func Round(val float64, precision int) float64 { if precision == 0 { return math.Round(val) } p := math.Pow10(precision) if precision < 0 { return math.Floor(val*p+0.5) * math.Pow10(-precision) } return math.Floor(val*p+0.5) / p }但是仍然无法避免这个问题,如果需要准确的运算,你或许可以试试 github.com/shopspring/decimal
示例
from lancet.
https://github.com/duke-git/lancet/blob/main/README_zh-CN.md?plain=1#L27 项目介绍里提到
只依赖 go 标准库和 golang.org/x
可以使用decimal包解决
func RoundToString[T constraints.Float | constraints.Integer](x T, n int) string { d, _ := decimal.NewFromString(convertor.ToString(x)) f, _ := d.Round(int32(n)).Float64() return strconv.FormatFloat(f, 'f', n, 64) }
浮点数运算基于 IEEE754 标准,丢精度可能是无法避免的
我看别人的代码有种精度更高的实现方式:func Round(val float64, precision int) float64 { if precision == 0 { return math.Round(val) } p := math.Pow10(precision) if precision < 0 { return math.Floor(val*p+0.5) * math.Pow10(-precision) } return math.Floor(val*p+0.5) / p }但是仍然无法避免这个问题,如果需要准确的运算,你或许可以试试 github.com/shopspring/decimal
示例
那就只能他自己用decimal包解决了
from lancet.
Related Issues (20)
- Feature Request: Read Large Files by Line and Support Seek HOT 1
- AesEcbDecrypt 以及slice的删除有问题: HOT 2
- 多线程安全 HOT 1
- stream Map Existing problem HOT 2
- maputil.MapTo在定义的struct中有指针时报panic: reflect: call of reflect.Value.Interface on zero Value HOT 2
- Documentation and Go playground seem broken HOT 2
- OS: windows, ARCH: 386 fail to build HOT 3
- strutil中是否可以增加SubBetween方法? HOT 4
- strutil.After() and strutil.Before() bug: length of source string less length of split char HOT 2
- Maputil.MapTo方法有误 HOT 2
- package /datastructure/set 'FromSlice' and 'Values' make me confuse HOT 1
- 受1brc项目影响,我认为可以来一个并发读取超大文本文件的工具函数 HOT 3
- Which branch should I submit to? HOT 3
- Maximum capacity overflows int HOT 1
- netutil.StructToUrlValues 当前不支持结构体嵌套转换 HOT 3
- 邮箱地址验证有误 HOT 2
- maputil.MapToStruct json标签中的omitempty会干扰转化 HOT 1
- 建议支持只导入一个包名,就可以使用所有函数的方式 HOT 1
- v2 版本里面structtomap 存在一个bug
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lancet.