Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

Round half to even in Go

Round half to even in Go

Rounding half to even is used a lot when dealing with financial transactions. Here is my implementation in Go:

import "math"

// http://en.wikipedia.org/wiki/Rounding#Round_half_to_even
func roundHalfToEven(f float64) float64 {
        integer, fraction := math.Modf(f)
        f = integer
        if 0.5 == math.Abs(fraction) {
                if 0 != math.Mod(f, 2) {
                        f += math.Copysign(1, integer)
                }
        } else {
                f += math.Copysign(float64(int(fraction+math.Copysign(0.5, fraction))), fraction)
        }
        return f
}

And here are unit tests:

import "testing"

func TestRoundHalfToEven(t *testing.T) {

        testCases := []struct {
                numberToRound  float64
                expectedResult float64
        }{
                {
                        // 23.5 =~ 24
                        numberToRound:  23.5,
                        expectedResult: 24,
                },
                {
                        // 24.5 =~ 24
                        numberToRound:  24.5,
                        expectedResult: 24,
                },
                {
                        // 7.58 =~ 8
                        numberToRound:  7.58,
                        expectedResult: 8,
                },
                {
                        // 7.46 =~ 7
                        numberToRound:  7.46,
                        expectedResult: 7,
                },
                {
                        // 5 =~ 5
                        numberToRound:  5,
                        expectedResult: 5,
                },
                {
                        // -23.5 =! -24
                        numberToRound:  -23.5,
                        expectedResult: -24,
                },
                {
                        // -24.5 =~ -24
                        numberToRound:  -24.5,
                        expectedResult: -24,
                },
                {
                        // -7.58 =~ -8
                        numberToRound:  -7.58,
                        expectedResult: -8,
                },
                {
                        // -7.46 =~ -7
                        numberToRound:  -7.46,
                        expectedResult: -7,
                },
                {
                        // -5 =~ -5
                        numberToRound:  -5,
                        expectedResult: -5,
                },
                {
                        // 0 =~ 0
                        numberToRound:  0,
                        expectedResult: 0,
                },
        }

        for i, tc := range testCases {
                actualResult := roundHalfToEven(tc.numberToRound)
                if actualResult != tc.expectedResult {
                        t.Errorf("%v rounded half to even should be %v, instead of %v (%v)", tc.numberToRound, tc.expectedResult, actualResult, i)
                }
        }

}

Source: http://blog.richardknop.com/2013/11/round-half-to-even-in-go/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Posted Listopad 25th, 2013 in Zend and PHP.

Leave a response: