From 390b2324bb9b301ff6eff42c891ffb36470d8f4a Mon Sep 17 00:00:00 2001 From: Alexandru Borza Date: Thu, 21 Dec 2023 06:34:12 +0200 Subject: [PATCH] BAEL-7286 Print a double value without scientific notation in Java (#15427) * BAEL-7051 - Catch Common Mistakes with Error-Prone Library in Java * BAEL-7286 Print a double value without scientific notation in Java * BAEL-7286 Print a double value without scientific notation in Java * BAEL-7286 Print a double value without scientific notation in Java * rename package * format code --- ...ubleWithoutScientificNotationUnitTest.java | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/withoutscientificnotation/PrintDoubleWithoutScientificNotationUnitTest.java diff --git a/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/withoutscientificnotation/PrintDoubleWithoutScientificNotationUnitTest.java b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/withoutscientificnotation/PrintDoubleWithoutScientificNotationUnitTest.java new file mode 100644 index 0000000000..a9e78c76a0 --- /dev/null +++ b/core-java-modules/core-java-numbers-7/src/test/java/com/baeldung/withoutscientificnotation/PrintDoubleWithoutScientificNotationUnitTest.java @@ -0,0 +1,113 @@ +package com.baeldung.withoutscientificnotation; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.util.Locale; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PrintDoubleWithoutScientificNotationUnitTest { + + private final PrintStream standardOut = System.out; + private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + + @BeforeEach + public void setUp() { + System.setOut(new PrintStream(outputStreamCaptor)); + Locale.setDefault(Locale.US); + } + + @AfterEach + public void tearDown() { + System.setOut(standardOut); + } + + @Test + void givenLargeNumber_whenPrintWithDecimalFormat_thenOutputIsWithoutScientificNotation() { + + DecimalFormat df = new DecimalFormat("#.###########"); + double largeNumber = 256450000d; + System.out.println("Large Number: " + df.format(largeNumber)); + + Assertions.assertEquals("Large Number: 256450000", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenSmallNumber_whenPrintWithDecimalFormat_thenOutputIsWithoutScientificNotation() { + + DecimalFormat df = new DecimalFormat("#.###########"); + double smallNumber = 0.0000046d; + System.out.println("Small Number: " + df.format(smallNumber)); + + Assertions.assertEquals("Small Number: 0.0000046", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenLargeNumber_whenPrintWithPrintf_thenOutputIsWithoutScientificNotation() { + + double largeNumber = 256450000d; + System.out.printf("Large Number: %.7f", largeNumber); + + Assertions.assertEquals("Large Number: 256450000.0000000", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenSmallNumber_whenPrintWithPrintf_thenOutputIsWithoutScientificNotation() { + + double smallNumber = 0.0000046d; + System.out.printf("Small Number: %.7f", smallNumber); + + Assertions.assertEquals("Small Number: 0.0000046", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenLargeNumber_whenPrintWithBigDecimal_thenOutputIsWithoutScientificNotation() { + + double largeNumber = 256450000d; + System.out.println("Large Number: " + BigDecimal.valueOf(largeNumber).toPlainString()); + + Assertions.assertEquals("Large Number: 256450000", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenSmallNumber_whenPrintWithBigDecimal_thenOutputIsWithoutScientificNotation() { + + double smallNumber = 0.0000046d; + System.out.println("Small Number: " + BigDecimal.valueOf(smallNumber).toPlainString()); + + Assertions.assertEquals("Small Number: 0.0000046", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenLargeNumber_whenPrintWithStringFormat_thenOutputIsWithoutScientificNotation() { + + double largeNumber = 256450000d; + String formattedLargeNumber = String.format("%.7f", largeNumber); + System.out.println("Large Number: " + formattedLargeNumber); + + Assertions.assertEquals("Large Number: 256450000.0000000", outputStreamCaptor.toString() + .trim()); + } + + @Test + void givenSmallNumber_whenPrintWithStringFormat_thenOutputIsWithoutScientificNotation() { + + double smallNumber = 0.0000046d; + String formattedSmallNumber = String.format("%.7f", smallNumber); + System.out.println("Small Number: " + formattedSmallNumber); + + Assertions.assertEquals("Small Number: 0.0000046", outputStreamCaptor.toString() + .trim()); + } +}