Test cases for float to BigDecimal conversion - first commit

This commit is contained in:
mcasari 2023-09-19 21:53:05 +02:00
parent 7aeb2821ff
commit 4ce8b0e70a
4 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,7 @@
## Shallow vs Deep Copy
This module contains an article about converting float to BigDecimal in Java
### Relevant articles:
- [Converting from Float to BigDecimal in Java](https://drafts.baeldung.com/?p=175240&preview=true)

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>convert-float-to-bigdecimal</artifactId>
<name>convert-float-to-bigdecimal</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,48 @@
package com.baeldung.convertfloattobigdecimal;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
class ConvertFloatToBigDecimalUnitTest {
@Test
public void whenFloatComparedWithDifferentValues_thenCouldMatch() {
assertNotEquals(1.1f, 1.09f);
assertEquals(1.1f, 1.09999999f);
}
@Test
public void whenCreatedFromFloat_thenCouldNotMatch() {
float floatToConvert = 0.5f;
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
assertEquals("0.5", bdFromFloat.toString());
floatToConvert = 1.1f;
bdFromFloat = new BigDecimal(floatToConvert);
assertNotEquals("1.1", bdFromFloat.toString());
}
@Test
public void whenCreatedFromString_thenMatches() {
String floatValue = Float.toString(1.1f);
BigDecimal bdFromString = new BigDecimal(floatValue);
assertEquals("1.1", bdFromString.toString());
}
@Test
public void whenCreatedByValueOfAndIsFloat_thenDoesNotMatch() {
float floatToConvert = 1.1f;
BigDecimal bdByValueOf = BigDecimal.valueOf(floatToConvert);
assertNotEquals("1.1", bdByValueOf.toString());
}
@Test
public void whenCreatedByValueOfAndIsDouble_thenMatches() {
double doubleToConvert = 1.1d;
BigDecimal bdByValueOf = BigDecimal.valueOf(doubleToConvert);
assertEquals("1.1", bdByValueOf.toString());
}
}