I'll add that I typically just use fmt.Sprintf("%v", x), which gives the shortest possible unambiguous string representation of the floating point value x.
On Wednesday, August 13, 2025 at 11:33:40 PM UTC+1 Jason E. Aten wrote: > Hi Jochen, > > I think you can get what you want simply with the fmt package. > > Consider fmt.Sprintf("%0.5f", x), for example, if you wanted 5 decimal > places. > > If you need to actually round a floating point number, rather than just > displaying > a certain number of places, the CockroachDB guys wrote a good summary of > the approaches here, back in 2017: > > https://www.cockroachlabs.com/blog/rounding-implementations-in-go/ > > As they mention, there is now https://pkg.go.dev/math#Round in the > standard > library, which would typically be used by: > > multiplying by a power of 10, math.Round()-ing, and then dividing by > the same power of 10. > > > > > On Wednesday, August 13, 2025 at 2:42:18 PM UTC+1 robert engels wrote: > >> A floating point number cannot represent all possible real numbers - so >> when you “shift it back” the number you expect may not be possible. >> >> See this on why 0.1 cannot be represented. >> https://how.dev/answers/why-does-01-not-exist-in-floating-point >> >> On Aug 13, 2025, at 7:55 AM, Jochen Voss <joche...@gmail.com> wrote: >> >> Dear Robert, >> >> Thank you for your response. To make sure I understand: Are you saying >> Claude's Round() function does not work? (For which example?) Or are you >> saying it is impossible to do better than Claude's function? >> >> Many thanks, >> Jochen >> >> On Wednesday, 13 August 2025 at 13:52:08 UTC+1 robert engels wrote: >> >>> Read up on numerical analysis - what you are asking for is impossible :) >>> >>> You need to convert to a string, or use BCD/fixed place values - like >>> github.com/robaho/fixed >>> >>> On Aug 13, 2025, at 7:42 AM, Jochen Voss <joche...@gmail.com> wrote: >>> >>> Dear all, >>> >>> I would like to define a function "func Round(x float64, digits int) >>> float64" which rounds to the given number of digits, in the sense that >>> I want "strconv.FormatFloat(x, 'f', -1, 64)" to show at most the given >>> number of digits after the decimal point. >>> >>> The following naive approach does not work: >>> >>> func Round(x float64, digits int) float64 { >>> eps := math.Pow10(-digits) >>> return math.Round(x/eps) * eps >>> } >>> >>> For example for rounding math.Pi to five digits I get "3.1415900000000003" >>> instead of "3.14159". https://go.dev/play/p/gRtHG6ZgTjj . >>> >>> Anthropic's Claude suggested the following: >>> >>> func Round(x float64, digits int) float64 { >>> if digits <= 0 { >>> pow := math.Pow10(-digits) >>> return math.Round(x/pow) * pow >>> } >>> >>> format := "%." + strconv.Itoa(digits) + "f" >>> s := fmt.Sprintf(format, x) >>> result, _ := strconv.ParseFloat(s, 64) >>> >>> return result >>> } >>> >>> This seems to work, but also seems quite inefficient. >>> >>> Is there a better way? >>> >>> All the best, >>> Jochen >>> >>> PS.: Here is some testing code for experimenting >>> https://go.dev/play/p/Xcd6fTvYend >>> >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts...@googlegroups.com. >>> To view this discussion visit >>> https://groups.google.com/d/msgid/golang-nuts/7d3ccdd4-d88b-4eba-8a36-02b51b7751e1n%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/golang-nuts/7d3ccdd4-d88b-4eba-8a36-02b51b7751e1n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> >> To view this discussion visit >> https://groups.google.com/d/msgid/golang-nuts/1e3872a4-6b05-4f03-9dd3-37d1b6955297n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/1e3872a4-6b05-4f03-9dd3-37d1b6955297n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/a6706c15-4ca6-41fd-9b91-621e31c54dcen%40googlegroups.com.