From fe9c78ceb8999b37477da54bf58566e5d4fe328c Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Thu, 28 Sep 2023 16:29:51 +0100 Subject: [PATCH 1/2] BAEL-6895 Add code for truncating a double to two decimal places --- .../TruncateDoubleUnitTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java diff --git a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java new file mode 100644 index 0000000000..d5881c5d66 --- /dev/null +++ b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.truncatedouble; + +import static org.junit.Assert.assertEquals; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.NumberFormat; + +import org.junit.Test; + +public class TruncateDoubleUnitTest { + + @Test + public void givenADouble_whenUsingDecimalFormat_truncateToTwoDecimalPlaces() { + DecimalFormat df = new DecimalFormat("#.##"); + df.setRoundingMode(RoundingMode.DOWN); + + double value = 1.55555555; + String truncated = df.format(value); + assertEquals("1.55", truncated); + + double negativeValue = -1.55555555; + String negativeTruncated = df.format(negativeValue); + assertEquals("-1.55", negativeTruncated); + } + + @Test + public void givenADouble_whenUsingNumberFormat_truncateToTwoDecimalPlaces() { + NumberFormat nf = NumberFormat.getNumberInstance(); + nf.setMaximumFractionDigits(2); + nf.setRoundingMode(RoundingMode.DOWN); + + double value = 1.55555555; + String truncated = nf.format(value); + assertEquals("1.55", truncated); + + double negativeValue = -1.55555555; + String negativeTruncated = nf.format(negativeValue); + assertEquals("-1.55", negativeTruncated); + } + + @Test + public void givenADouble_whenUsingBigDecimal_truncateToTwoDecimalPlaces() { + BigDecimal positive = new BigDecimal(2.555555).setScale(2, RoundingMode.DOWN); + BigDecimal negative = new BigDecimal(-2.555555).setScale(2, RoundingMode.DOWN); + assertEquals("2.55", positive.toString()); + assertEquals("-2.55", negative.toString()); + } + + @Test + public void givenADouble_whenUsingMath_truncateToTwoDecimalPlaces() { + double positive = 1.55555555; + double truncated = Math.floor(positive * 100) / 100; + assertEquals("1.55", String.valueOf(truncated)); + + double negative = -1.55555555; + double negativeTruncated = Math.ceil(negative * 100) / 100; + assertEquals("-1.55", String.valueOf(negativeTruncated)); + } + + @Test + public void givenADouble_whenUsingStringFormat_truncateToTwoDecimalPlaces() { + double value = 1.55555555; + String truncated = String.format("%.2f", value); + assertEquals("1.56", truncated); + } + +} From d4d4a0e1a408eb9a2d9924101de37ab7919ec861 Mon Sep 17 00:00:00 2001 From: Sam Gardner Date: Wed, 11 Oct 2023 11:09:45 +0100 Subject: [PATCH 2/2] BAEL-6895 extend String format test to cover negatives --- .../baeldung/truncatedouble/TruncateDoubleUnitTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java index d5881c5d66..1bbca4069f 100644 --- a/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java +++ b/core-java-modules/core-java-numbers-6/src/test/java/com/baeldung/truncatedouble/TruncateDoubleUnitTest.java @@ -61,9 +61,13 @@ public class TruncateDoubleUnitTest { @Test public void givenADouble_whenUsingStringFormat_truncateToTwoDecimalPlaces() { - double value = 1.55555555; - String truncated = String.format("%.2f", value); + double positive = 1.55555555; + String truncated = String.format("%.2f", positive); assertEquals("1.56", truncated); + + double negative = -1.55555555; + String negativeTruncated = String.format("%.2f", negative); + assertEquals("-1.56", negativeTruncated); } }