Skip to content

math.Round is calculate correct but return wrong value #74556

Closed as not planned
Closed as not planned
@FocusHuang

Description

@FocusHuang

Go version

go version go1.23.4 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=D:\Enviornment\GO\src\cache
set GOENV=C:\Users\admin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Workspace\GO\pkg\mod
set GONOPROXY=gitee.com
set GONOSUMDB=gitee.com
set GOOS=windows
set GOPATH=D:\Workspace\GO
set GOPRIVATE=gitee.com
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Enviornment\GO
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Enviornment\GO\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.4
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\admin\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\Workspace\GO\src\gitlab\readboy\wrong-question-book\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\admin\AppData\Local\Temp\go-build2290248184=/tmp/go-build -gno-record-gcc-switches

What did you do?

package utils_test

import (
	"fmt"
	"math"
	"testing"
)

func TestToPercentage(t *testing.T) {
	testcases := []struct {
		value float64
		digit int
		want  string
	}{
		// {0.123456, 2, "12.35%"},
		// {0, 0, "0"},
		// {0.123456, 4, "12.3456%"},
		// {0.5, 0, "50%"},
		// {0.5, 1, "50%"},
		// {1, 0, "100%"},
		{0.5666666666666667, 0, "57%"},
	}
	for idx, tc := range testcases {
		got1 := ToPercentage(tc.value, tc.digit)
		got2 := ToPercentage2(tc.value, tc.digit)
		if got1 != got2 {
			t.Errorf("%d. ToPercentage(%v) = %s, value1: %s, value2: %s", idx, tc.value, tc.want, got1, got2)
		}
	}
}

func ToPercentage(value float64, n int) string {
	if value == 0 {
		return "0"
	}
	r2 := fmt.Sprintf("%v%%", math.Round(value*math.Pow10(n+2))/math.Pow10(n))
	return r2
}

func ToPercentage2(value float64, n int) string {
	if value == 0 {
		return "0"
	}
	return fmt.Sprintf("%v%%", Round(value, n+2)*100)
}

func Round(value float64, digit int) float64 {
	return math.Round(value*math.Pow10(digit)) / math.Pow10(digit)
}

What did you see happen?

--- FAIL: TestToPercentage (0.00s)
    d:\Workspace\GO\src\gitlab\readboy\wrong-question-book\utils\ratio_test.go:27: 0. ToPercentage(0.5666666666666667) = 57%, value1: 57%, value2: 56.99999999999999%
FAIL
FAIL	wrong-question-book/utils	0.652s
FAIL

What did you expect to see?

the testcase is correct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions