sap-labs-france / ev-server

The Open e-Mobility Charging Station management backend server (check also ev-dashboard and ev-mobile)
https://open-e-mobility.fr/
Apache License 2.0
141 stars 132 forks source link

Smart Charging does not use BigDecimal class!!! #4158

Open LucasBrazi06 opened 1 year ago

LucasBrazi06 commented 1 year ago

Error 400 in calling the Smart Charging java web service: image

3 x 67.124 = 201.372 so the Smart Charging service should not throw an exception!

In JS you can reproduce the error that may occur in Java:

image

Actions: -> Smart Charging Java has to be fixed -> All operation in Smart Charging integration in NodeJs has to use Decimal.Js or round to integer all the values -> Unit tests have to be adjusted accordingly

LucasBrazi06 commented 1 year ago

The Java code:

image

import java.math.*;

public class Main {
  public static void main(String[] args) {
    double maxCurrentPerPhase = 67.124;
    int sumUsedPhases = 3;
    double expectedResult = 201.372;
    // Without Big Decimal
    System.out.println("Without BigDecimal");
    double result1 = maxCurrentPerPhase * sumUsedPhases;
    System.out.println("26.4 * 3 = " + result1);
    System.out.println("Equals to expected result = " + (result1 == expectedResult));
    // With Big Decimal
    System.out.println("With BigDecimal");
    double result2 = new BigDecimal(maxCurrentPerPhase).multiply(new BigDecimal(sumUsedPhases), new MathContext(16)).doubleValue();
    System.out.println("26.4 * 3 = " + result2);
    System.out.println("Equals to expected result = " + (result2 == expectedResult));
  }
}