Home > Net >  How to perform precission deviding in Golang equal to BigDecimal.devide(BigDecimal divisor, MathCont
How to perform precission deviding in Golang equal to BigDecimal.devide(BigDecimal divisor, MathCont

Time:11-03

I've a task to migrate core banking system from java to golang, but i get stuck when i try to perform precission deviding in golang. I've try with math/ package rounding function but i don't get my desired result. I think the problem is i can't find golang dividing function that equal to BigDecimal.devide(BigDecimal divisor, MathContext mc) in java.

can anyone help me to get my desired result, please..

Big Thanks..

Here's my java code and it's desired result :


public BigDecimal getGrossInterest(BigDecimal a, BigDecimal b, Integer c) {
    BigDecimal d = a.multiply(b).multiply(new BigDecimal(c));
    BigDecimal e = d.divide(new BigDecimal(365), MathContext.DECIMAL32);
    BigDecimal result = e.setScale(0, RoundingMode.HALF_UP);
    return result;
}

@Test
void TestGetGrossInterest() {
    BigDecimal a1 = new BigDecimal(100000000);
    BigDecimal b1 = new BigDecimal(0.045);
    Integer c1 = Integer.valueOf(28);

    BigDecimal result1 = getGrossInterest(a1, b1, c1);
    System.out.println(result1); // result1 is 345206

    BigDecimal a2 = new BigDecimal(10000000);
    BigDecimal b2 = new BigDecimal(0.035);
    Integer c2 = Integer.valueOf(16);

    BigDecimal result2 = getGrossInterest(a2, b2, c2);
    System.out.println(result2); // result2 is 15342
}

and my golang code with wrong result as showed below.


func getGrossInterest(a float64, b float64, c int) float64 {
    d := a * b * float64(c)
    e := d / 365
    return math.Round(e)
}

func TestGetGrossInterest(t *testing.T) {
    a1 := 100000000.00
    b1 := 0.045
    c1 := 28

    result1 := getGrossInterest(a1, b1, c1)
    fmt.Printf("result1: %f\n", result1) 
    assert.Equal(t, float64(345206), result1) // failed test because result1 is 345205

    a2 := 10000000.00
    b2 := 0.035
    c2 := 16

    result2 := getGrossInterest(a2,b2,c2)
    fmt.Printf("result2: %f\n", result2)
    assert.Equal(t, float64(15342), result2)
}

CodePudding user response:

I would recommend to use enter image description here

CodePudding user response:

@2r2w thanks for your answer.

I've try func (Decimal) DivRound, but i don't get my desired result.

here's my code :

Java


    @Test
    void test() {
        BigDecimal a = BigDecimal.valueOf(125999999.99999999533706329657434253022074699401855468750000000000);
        BigDecimal b = BigDecimal.valueOf(5600000.0000000005329070518200751394033432006835937500000000000);

        BigDecimal resultA = a.divide(new BigDecimal(365), MathContext.DECIMAL32);
        System.out.println(resultA); // resultA is 345205.5

        BigDecimal resultB = b.divide(new BigDecimal(365), MathContext.DECIMAL32);
        System.out.println(resultB); // resultB is 15342.47
    }

Golang


func TestXxx(t *testing.T) {
    a := decimal.NewFromFloat(125999999.99999999533706329657434253022074699401855468750000000000)
    b := decimal.NewFromFloat(5600000.0000000005329070518200751394033432006835937500000000000)

    resultAWithPrec1 := a.DivRound(decimal.NewFromInt(365), 1)
    fmt.Printf("%v\n", resultAWithPrec1.String()) // result is 345205.5 (Correct)

    resultBWithPrec1 := b.DivRound(decimal.NewFromInt(365), 1)
    fmt.Printf("%v\n", resultBWithPrec1.String()) // result is 15342.5 (Incorrect)

    resultAWithPrec2 := a.DivRound(decimal.NewFromInt(365), 2)
    fmt.Printf("%v\n", resultAWithPrec2.String()) // result is 345205.48 (Incorrect)

    resultBWithPrec2 := b.DivRound(decimal.NewFromInt(365), 2)
    fmt.Printf("%v\n", resultBWithPrec2.String()) // result is 15342.47 (Correct)
}

I think the problem is :

java can produce different count of decimal number (345205.5 and 15342.47), while on golang i must define specific count of decimal number.

how to achieve the the same result ?

  • Related