Javadoublevsbigdecimal (#14862)

* Create BigDecimalConversionUnitTest.java

* Add files via upload

* Update BigDecimalConversionUnitTest.java

* Update BigDecimalConversionUnitTest.java

* Update BigDecimalConversionUnitTest.java
This commit is contained in:
Rufina Uche 2023-10-09 08:53:46 +01:00 committed by GitHub
parent b7f3161bc5
commit 0e7c032aa8
3 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalConversionUnitTest {
@Test
void whenConvertingDoubleToBigDecimal_thenConversionIsCorrect() {
double doubleValue = 123.456;
BigDecimal bigDecimalValue = BigDecimal.valueOf(doubleValue);
BigDecimal expected = new BigDecimal("123.456").setScale(3, RoundingMode.HALF_UP);
assertEquals(expected, bigDecimalValue.setScale(3, RoundingMode.HALF_UP));
}
@Test
void givenDecimalPlacesGreaterThan15_whenConvertingBigDecimalToDouble_thenPrecisionIsLost() {
BigDecimal bigDecimalValue = new BigDecimal("789.1234567890123456");
double doubleValue = bigDecimalValue.doubleValue();
BigDecimal convertedBackToBigDecimal = BigDecimal.valueOf(doubleValue);
assertNotEquals(bigDecimalValue, convertedBackToBigDecimal);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.junit.jupiter.api.Test;
public class BigDecimalUnitTest {
private BigDecimal bigDecimal1 = new BigDecimal("124567890.0987654321");
private BigDecimal bigDecimal2 = new BigDecimal("987654321.123456789");
@Test
public void givenTwoBigDecimals_whenAdd_thenCorrect() {
BigDecimal expected = new BigDecimal("1112222211.2222222211");
BigDecimal actual = bigDecimal1.add(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenMultiply_thenCorrect() {
BigDecimal expected = new BigDecimal("123030014929277547.5030955772112635269");
BigDecimal actual = bigDecimal1.multiply(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenSubtract_thenCorrect() {
BigDecimal expected = new BigDecimal("-863086431.0246913569");
BigDecimal actual = bigDecimal1.subtract(bigDecimal2);
assertEquals(expected, actual);
}
@Test
public void givenTwoBigDecimals_whenDivide_thenCorrect() {
BigDecimal expected = new BigDecimal("0.13");
BigDecimal actual = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
assertEquals(expected, actual);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.javadoublevsbigdecimal;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class JavaDoubleUnitTest {
@Test
public void givenDoubleLiteral_whenAssigningToDoubleVariable_thenValueIsNotExactlyEqual() {
double doubleValue = 0.1;
double epsilon = 0.0000000000000001;
assertEquals(0.1, doubleValue, epsilon);
}
}