|
| 1 | +package com.baeldung.decimalformat; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.text.DecimalFormat; |
| 7 | +import java.text.DecimalFormatSymbols; |
| 8 | +import java.text.NumberFormat; |
| 9 | +import java.text.ParseException; |
| 10 | +import java.util.Locale; |
| 11 | + |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class DecimalFormatExamplesTest { |
| 15 | + |
| 16 | + double d = 1234567.89; |
| 17 | + |
| 18 | + @Test |
| 19 | + public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() { |
| 20 | + |
| 21 | + assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 22 | + .isEqualTo("1234567.89"); |
| 23 | + |
| 24 | + assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 25 | + .isEqualTo("1234567.89"); |
| 26 | + |
| 27 | + assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 28 | + .isEqualTo("1234567.89"); |
| 29 | + |
| 30 | + assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 31 | + .isEqualTo("001234567.890"); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() { |
| 37 | + |
| 38 | + assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9"); |
| 39 | + |
| 40 | + assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568"); |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() { |
| 46 | + |
| 47 | + assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 48 | + .isEqualTo("1,234,567.9"); |
| 49 | + |
| 50 | + assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 51 | + .isEqualTo("1,234,568"); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() { |
| 57 | + |
| 58 | + assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 59 | + .isEqualTo("The 1234568 number"); |
| 60 | + |
| 61 | + assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 62 | + .isEqualTo("The # 1234568 number"); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void givenLocales_WhenFormatting_ThenCorrectOutput() { |
| 68 | + |
| 69 | + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 70 | + .isEqualTo("1,234,567.89"); |
| 71 | + |
| 72 | + assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d)) |
| 73 | + .isEqualTo("1.234.567,89"); |
| 74 | + |
| 75 | + assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d)) |
| 76 | + .isEqualTo("1.234.567,89"); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void givenE_WhenFormatting_ThenScientificNotation() { |
| 82 | + |
| 83 | + assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 84 | + .isEqualTo("12.3456789E5"); |
| 85 | + |
| 86 | + assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 87 | + .isEqualTo("123.456789E4"); |
| 88 | + |
| 89 | + assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 90 | + .isEqualTo("1.23456789E6"); |
| 91 | + |
| 92 | + assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)) |
| 93 | + .isEqualTo("1.23456789E6"); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException { |
| 99 | + |
| 100 | + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89")) |
| 101 | + .isEqualTo(1234567.89); |
| 102 | + |
| 103 | + assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89")) |
| 104 | + .isEqualTo(1234567.89); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException { |
| 110 | + |
| 111 | + NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)); |
| 112 | + ((DecimalFormat) nf).setParseBigDecimal(true); |
| 113 | + assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89)); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments