BAEL-6846 - Convert from float to BigDecimal
This commit is contained in:
parent
4ce8b0e70a
commit
a7940d9658
|
@ -1,7 +0,0 @@
|
|||
## 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)
|
|
@ -1,16 +0,0 @@
|
|||
<?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>
|
|
@ -1,14 +0,0 @@
|
|||
<?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>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.convertfloattobigdecimal;
|
||||
package com.baeldung.floattobigdecimal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
@ -7,7 +7,7 @@ import java.math.BigDecimal;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConvertFloatToBigDecimalUnitTest {
|
||||
class FloatToBigDecimalUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFloatComparedWithDifferentValues_thenCouldMatch() {
|
||||
|
@ -16,12 +16,16 @@ class ConvertFloatToBigDecimalUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedFromFloat_thenCouldNotMatch() {
|
||||
public void whenCreatedFromCertainFloatValues_thenMatches() {
|
||||
float floatToConvert = 0.5f;
|
||||
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
|
||||
assertEquals("0.5", bdFromFloat.toString());
|
||||
floatToConvert = 1.1f;
|
||||
bdFromFloat = new BigDecimal(floatToConvert);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedFromCertainFloatValues_thenDoesNotMatch() {
|
||||
float floatToConvert = 1.1f;
|
||||
BigDecimal bdFromFloat = new BigDecimal(floatToConvert);
|
||||
assertNotEquals("1.1", bdFromFloat.toString());
|
||||
}
|
||||
|
||||
|
@ -39,10 +43,18 @@ class ConvertFloatToBigDecimalUnitTest {
|
|||
assertNotEquals("1.1", bdByValueOf.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloatCastToDouble_thenGotADifferentNumber() {
|
||||
float floatToConvert = 1.1f;
|
||||
double doubleCast = floatToConvert;
|
||||
assertEquals("1.100000023841858", Double.toString(doubleCast));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatedByValueOfAndIsDouble_thenMatches() {
|
||||
double doubleToConvert = 1.1d;
|
||||
BigDecimal bdByValueOf = BigDecimal.valueOf(doubleToConvert);
|
||||
assertEquals("1.1", bdByValueOf.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
## Shallow vs Deep Copy
|
||||
|
||||
This module contains an article about shallow vs deep copy of Java objects
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Creating a deep vs shallow copy of an object in Java](https://drafts.baeldung.com/?p=172294&preview=true)
|
|
@ -1,16 +0,0 @@
|
|||
<?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>shallow-deep-copy</artifactId>
|
||||
<name>shallow-deep-copy</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
|
@ -1,23 +0,0 @@
|
|||
package com.baeldung.shallowdeepcopy;
|
||||
|
||||
public class Dependency implements Cloneable {
|
||||
private int value = 13;
|
||||
|
||||
public Dependency(int value) {
|
||||
super();
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.baeldung.shallowdeepcopy;
|
||||
|
||||
public class Main implements Cloneable {
|
||||
private Dependency dependency;
|
||||
|
||||
public Main(Dependency dependency) {
|
||||
super();
|
||||
this.dependency = dependency;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
}
|
||||
|
||||
public Object deepClone() throws CloneNotSupportedException {
|
||||
Main dependentClone = (Main) super.clone();
|
||||
Dependency dependencyClone = (Dependency) this.dependency.clone();
|
||||
dependentClone.setDependency(dependencyClone);
|
||||
return dependentClone;
|
||||
}
|
||||
|
||||
public Dependency getDependency() {
|
||||
return dependency;
|
||||
}
|
||||
|
||||
public void setDependency(Dependency dependency) {
|
||||
this.dependency = dependency;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
<?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>
|
|
@ -1,28 +0,0 @@
|
|||
package com.baeldung.shallowdeepcopy;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ShallowDeepCopyUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnObjectAndADependency_whenShallowClonedAndDepencyChanges_ThenDepencyHasNewValue() throws Exception {
|
||||
Dependency dependency = new Dependency(13);
|
||||
Main main = new Main(dependency);
|
||||
Main shallowClone = (Main) main.clone();
|
||||
dependency.setValue(17);
|
||||
assertEquals(17, shallowClone.getDependency()
|
||||
.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnObjectAndADependency_whenDeepClonedAndDepencyChanges_ThenDepencyKeepsOldValue() throws Exception {
|
||||
Dependency dependency = new Dependency(13);
|
||||
Main main = new Main(dependency);
|
||||
Main deepClone = (Main) main.deepClone();
|
||||
dependency.setValue(17);
|
||||
assertEquals(13, deepClone.getDependency()
|
||||
.getValue());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue