Am trying to round up 2 decimal places using a method I wrote in my class.
pom.xml:
<?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>
<groupId>com.mathutils</groupId>
<artifactId>mathutils</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.20.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
MathUtils.java:
public class MathUtils {
public static double roundUp(double value) {
DecimalFormat df = new DecimalFormat("#.##");
return(Double.valueOf(df.format(value)));
}
}
MathUtilsTest.java:
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MathUtilsTest {
@Test
public void roundUp() {
assertThat(MathUtils.roundUp(0.565)).isEqualTo(0.57); // this needs to be 57 but actual was .56
assertThat(MathUtils.roundUp(0.5649)).isEqualTo(0.56); // works
}
}
Question(s):
- Need 0.0565 to round up to 0.57 but fails with this:
org.opentest4j.AssertionFailedError:
expected: 0.57
but was: 0.56
Expected :0.57
Actual :0.56