Restructured test using AsserJ library
This commit is contained in:
parent
f05224a5a7
commit
6f496d0d5c
|
@ -7,30 +7,6 @@ import java.text.NumberFormat;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class FormatNumber {
|
public class FormatNumber {
|
||||||
private static final double D = 4.2352989244d;
|
|
||||||
private static final double F = 8.6994540927d;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println(D + " with Big Decimal (2 places) is " + withBigDecimal(D, 2));
|
|
||||||
System.out.println(D + " with Big Decimal (3 places) is " + withBigDecimal(D, 3));
|
|
||||||
System.out.println(F + " with Big Decimal (2 places) is " + withBigDecimal(F, 2));
|
|
||||||
System.out.println(F + " with Big Decimal (3 places) is " + withBigDecimal(F, 3));
|
|
||||||
System.out.println(D + " with Math.round is (2 places) " + withMathRound(D, 2));
|
|
||||||
System.out.println(D + " with Math.round is (3 places) " + withMathRound(D, 3));
|
|
||||||
System.out.println(F + " with Math.round is (2 places) " + withMathRound(F, 2));
|
|
||||||
System.out.println(F + " with Math.round is (3 places) " + withMathRound(F, 3));
|
|
||||||
System.out.println(D + " with String Format is (2 places) " + withStringFormat(D, 2));
|
|
||||||
System.out.println(D + " with String Format is (3 places) " + withStringFormat(D, 3));
|
|
||||||
System.out.println(F + " with String Format is (2 places) " + withStringFormat(F, 2));
|
|
||||||
System.out.println(F + " with String Format is (3 places) " + withStringFormat(F, 3));
|
|
||||||
System.out.println(D + " with Decimal Format (local) is " + withDecimalFormatLocal(D));
|
|
||||||
System.out.println(D + " with Decimal Format (2 places) is " + withDecimalFormatPattern(D, 2));
|
|
||||||
System.out.println(D + " with Decimal Format (3 places) is " + withDecimalFormatPattern(D, 3));
|
|
||||||
System.out.println(F + " with Decimal Format is (local) " + withDecimalFormatLocal(F));
|
|
||||||
System.out.println(F + " with Decimal Format is (2 places) " + withDecimalFormatPattern(F, 2));
|
|
||||||
System.out.println(F + " with Decimal Format is (3 places) " + withDecimalFormatPattern(F, 3));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double withBigDecimal(double value, int places) {
|
public static double withBigDecimal(double value, int places) {
|
||||||
if (places < 0)
|
if (places < 0)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|
|
@ -1,42 +1,37 @@
|
||||||
package com.baeldung.formatNumber;
|
package com.baeldung.formatNumber;
|
||||||
|
|
||||||
import org.apache.commons.math3.util.Precision;
|
|
||||||
import org.decimal4j.util.DoubleRounder;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.baeldung.formatNumber.FormatNumber.*;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
public class FormatNumberTest {
|
public class FormatNumberTest {
|
||||||
private double value = 2.03456d;
|
private static final double D = 4.2352989244d;
|
||||||
private int places = 2;
|
private static final double F = 8.6994540927d;
|
||||||
private double expected = 2.03d;
|
|
||||||
|
|
||||||
@Test public void givenDecimalNumber_whenFormatNumberToNDecimalPlaces_thenGetExpectedResult() {
|
@Test public void givenDecimalNumber_whenFormatNumberToNDecimalPlaces_thenGetExpectedResult() {
|
||||||
double delta = 0.0d;
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta);
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
|
||||||
|
|
||||||
places = 3;
|
assertThat(withBigDecimal(D, 2)).isEqualTo(4.24);
|
||||||
expected = 2.035d;
|
assertThat(withBigDecimal(D, 3)).isEqualTo(4.235);
|
||||||
|
assertThat(withBigDecimal(F, 2)).isEqualTo(8.7);
|
||||||
|
assertThat(withBigDecimal(F, 3)).isEqualTo(8.699);
|
||||||
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
assertThat(withMathRound(D, 2)).isEqualTo(4.24);
|
||||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta);
|
assertThat(withMathRound(D, 3)).isEqualTo(4.235);
|
||||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
assertThat(withMathRound(F, 2)).isEqualTo(8.7);
|
||||||
|
assertThat(withMathRound(F, 3)).isEqualTo(8.699);
|
||||||
|
|
||||||
value = 256.024d;
|
assertThat(withStringFormat(D, 2)).isEqualTo("4.24");
|
||||||
places = 2;
|
assertThat(withStringFormat(D, 3)).isEqualTo("4.235");
|
||||||
expected = 256.02d;
|
assertThat(withStringFormat(F, 2)).isEqualTo("8.70");
|
||||||
|
assertThat(withStringFormat(F, 3)).isEqualTo("8.699");
|
||||||
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
assertThat(withDecimalFormatLocal(D)).isEqualTo(4.235);
|
||||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta);
|
assertThat(withDecimalFormatLocal(F)).isEqualTo(8.699);
|
||||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
|
||||||
|
|
||||||
value = 260.773d;
|
assertThat(withDecimalFormatPattern(D, 2)).isEqualTo(4.24);
|
||||||
places = 2;
|
assertThat(withDecimalFormatPattern(D, 3)).isEqualTo(4.235);
|
||||||
expected = 260.77d;
|
assertThat(withDecimalFormatPattern(F, 2)).isEqualTo(8.7);
|
||||||
|
assertThat(withDecimalFormatPattern(F, 3)).isEqualTo(8.699);
|
||||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormatPattern(value, places), delta);
|
|
||||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue