Git Product home page Git Product logo

Comments (4)

cannian1 avatar cannian1 commented on May 26, 2024

@chengfny

浮点数运算基于 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.

pigwantacat avatar pigwantacat commented on May 26, 2024

可以使用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)
}

@chengfny

浮点数运算基于 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.

cannian1 avatar cannian1 commented on May 26, 2024

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)
}

@chengfny

浮点数运算基于 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.

pigwantacat avatar pigwantacat commented on May 26, 2024

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)
}

@chengfny

浮点数运算基于 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.