From 3396e92d2d7b8edb7aa9b5d25f5d6514f02ffd64 Mon Sep 17 00:00:00 2001 From: Marcin Krykowski Date: Sat, 30 May 2020 05:32:54 +0100 Subject: [PATCH] Split up test method into smaller ones. Correct code formatting --- .../baeldung/formatNumber/FormatNumber.java | 56 +++++++++---------- .../formatNumber/FormatNumberUnitTest.java | 56 +++++++++++-------- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/java-numbers-3/src/main/java/com/baeldung/formatNumber/FormatNumber.java b/java-numbers-3/src/main/java/com/baeldung/formatNumber/FormatNumber.java index 3470a8419a..6a16fffa7e 100644 --- a/java-numbers-3/src/main/java/com/baeldung/formatNumber/FormatNumber.java +++ b/java-numbers-3/src/main/java/com/baeldung/formatNumber/FormatNumber.java @@ -7,38 +7,38 @@ import java.text.NumberFormat; import java.util.Locale; public class FormatNumber { - public static double withBigDecimal(double value, int places) { - if (places < 0) - throw new IllegalArgumentException(); + public static double withBigDecimal(double value, int places) { + if (places < 0) + throw new IllegalArgumentException(); - BigDecimal bigDecimal = new BigDecimal(value); - bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP); - return bigDecimal.doubleValue(); - } + BigDecimal bigDecimal = new BigDecimal(value); + bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP); + return bigDecimal.doubleValue(); + } - public static double withMathRound(double value, int places) { - double scale = Math.pow(10, places); - return Math.round(value * scale) / scale; - } + public static double withMathRound(double value, int places) { + double scale = Math.pow(10, places); + return Math.round(value * scale) / scale; + } - public static double withDecimalFormatPattern(double value, int places) { - DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00"); - DecimalFormat df3 = new DecimalFormat("#,###,###,##0.000"); - if (places == 2) - return new Double(df2.format(value)); - else if (places == 3) - return new Double(df3.format(value)); - else - throw new IllegalArgumentException(); - } + public static double withDecimalFormatPattern(double value, int places) { + DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00"); + DecimalFormat df3 = new DecimalFormat("#,###,###,##0.000"); + if (places == 2) + return new Double(df2.format(value)); + else if (places == 3) + return new Double(df3.format(value)); + else + throw new IllegalArgumentException(); + } - public static double withDecimalFormatLocal(double value) { - DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); - return new Double(df.format(value)); - } + public static double withDecimalFormatLocal(double value) { + DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); + return new Double(df.format(value)); + } - public static String withStringFormat(double value, int places) { - return String.format("%." + places + "f", value); - } + public static String withStringFormat(double value, int places) { + return String.format("%." + places + "f", value); + } } diff --git a/java-numbers-3/src/test/java/com/baeldung/formatNumber/FormatNumberUnitTest.java b/java-numbers-3/src/test/java/com/baeldung/formatNumber/FormatNumberUnitTest.java index 4eb6e42f1a..ef3700e27a 100644 --- a/java-numbers-3/src/test/java/com/baeldung/formatNumber/FormatNumberUnitTest.java +++ b/java-numbers-3/src/test/java/com/baeldung/formatNumber/FormatNumberUnitTest.java @@ -6,33 +6,41 @@ import static com.baeldung.formatNumber.FormatNumber.*; import static org.assertj.core.api.Assertions.*; public class FormatNumberUnitTest { - private static final double D = 4.2352989244d; - private static final double F = 8.6994540927d; + private static final double D = 4.2352989244d; + private static final double F = 8.6994540927d; - @Test - public void givenDecimalNumber_whenFormatNumberToNDecimalPlaces_thenGetExpectedResult() { + @Test + public void givenDecimalNumber_whenFormatNumber_withBigDecimal_thenGetExpectedResult() { + assertThat(withBigDecimal(D, 2)).isEqualTo(4.24); + assertThat(withBigDecimal(D, 3)).isEqualTo(4.235); + assertThat(withBigDecimal(F, 2)).isEqualTo(8.7); + assertThat(withBigDecimal(F, 3)).isEqualTo(8.699); + } - assertThat(withBigDecimal(D, 2)).isEqualTo(4.24); - assertThat(withBigDecimal(D, 3)).isEqualTo(4.235); - assertThat(withBigDecimal(F, 2)).isEqualTo(8.7); - assertThat(withBigDecimal(F, 3)).isEqualTo(8.699); + @Test + public void givenDecimalNumber_whenFormatNumber_withDecimalFormat_thenGetExpectedResult() { + assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235); + assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699); - assertThat(withMathRound(D, 2)).isEqualTo(4.24); - assertThat(withMathRound(D, 3)).isEqualTo(4.235); - assertThat(withMathRound(F, 2)).isEqualTo(8.7); - assertThat(withMathRound(F, 3)).isEqualTo(8.699); + assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24); + assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235); + assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7); + assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699); + } - assertThat(withStringFormat(D, 2)).isEqualTo("4.24"); - assertThat(withStringFormat(D, 3)).isEqualTo("4.235"); - assertThat(withStringFormat(F, 2)).isEqualTo("8.70"); - assertThat(withStringFormat(F, 3)).isEqualTo("8.699"); + @Test + public void givenDecimalNumber_whenFormatNumber_withStringFormat_thenGetExpectedResult() { + assertThat(withStringFormat(D, 2)).isEqualTo("4.24"); + assertThat(withStringFormat(D, 3)).isEqualTo("4.235"); + assertThat(withStringFormat(F, 2)).isEqualTo("8.70"); + assertThat(withStringFormat(F, 3)).isEqualTo("8.699"); + } - assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235); - assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699); - - assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24); - assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235); - assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7); - assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699); - } + @Test + public void givenDecimalNumber_whenFormatNumber_withMathRound_thenGetExpectedResult() { + assertThat(withMathRound(D, 2)).isEqualTo(4.24); + assertThat(withMathRound(D, 3)).isEqualTo(4.235); + assertThat(withMathRound(F, 2)).isEqualTo(8.7); + assertThat(withMathRound(F, 3)).isEqualTo(8.699); + } }