Add formating methods for percentages, currencies and big numbers
This commit is contained in:
parent
3396e92d2d
commit
e3f7cf6b2c
|
@ -40,5 +40,34 @@ public class FormatNumber {
|
||||||
public static String withStringFormat(double value, int places) {
|
public static String withStringFormat(double value, int places) {
|
||||||
return String.format("%." + places + "f", value);
|
return String.format("%." + places + "f", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String byPaddingOutZeros(int value, int paddingLength) {
|
||||||
|
return String.format("%0" + paddingLength + "d", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double withTwoDecimalPlaces(double value) {
|
||||||
|
DecimalFormat df = new DecimalFormat("#.00");
|
||||||
|
return new Double(df.format(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String withLongNumbers(double value) {
|
||||||
|
DecimalFormat df = new DecimalFormat("###,###,###");
|
||||||
|
return df.format(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String forPercentages(double value, Locale localisation) {
|
||||||
|
NumberFormat nf = NumberFormat.getPercentInstance(localisation);
|
||||||
|
return nf.format(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String currencyWithChosenLocalisation(double value, Locale localisation) {
|
||||||
|
NumberFormat nf = NumberFormat.getCurrencyInstance(localisation);
|
||||||
|
return nf.format(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String currencyWithDefaultLocalisation(double value) {
|
||||||
|
NumberFormat nf = NumberFormat.getCurrencyInstance();
|
||||||
|
return nf.format(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package com.baeldung.formatNumber;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import static com.baeldung.formatNumber.FormatNumber.*;
|
import static com.baeldung.formatNumber.FormatNumber.*;
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
@ -43,4 +45,44 @@ public class FormatNumberUnitTest {
|
||||||
assertThat(withMathRound(F, 2)).isEqualTo(8.7);
|
assertThat(withMathRound(F, 2)).isEqualTo(8.7);
|
||||||
assertThat(withMathRound(F, 3)).isEqualTo(8.699);
|
assertThat(withMathRound(F, 3)).isEqualTo(8.699);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntegerNumber_whenFormatNumber_byPaddingOutZeros_thenGetExpectedResult() {
|
||||||
|
int value = 1;
|
||||||
|
assertThat(byPaddingOutZeros(value, 3)).isEqualTo("001");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntegerNumber_whenFormatNumber_withTwoDecimalPlaces_thenGetExpectedResult() {
|
||||||
|
int value = 12;
|
||||||
|
assertThat(withTwoDecimalPlaces(value)).isEqualTo(12.00);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIntegerNumber_whenFormatNumber_withLongNumbers_thenGetExpectedResult() {
|
||||||
|
int value = 123456789;
|
||||||
|
assertThat(withLongNumbers(value)).isEqualTo("123,456,789");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDecimalNumber_whenFormatNumber_forPercentages_thenGetExpectedResult() {
|
||||||
|
double value = 25f / 100f;
|
||||||
|
assertThat(forPercentages(value, new Locale("en", "US"))).isEqualTo("25%");
|
||||||
|
assertThat(forPercentages(value, new Locale("pl", "PL"))).isEqualTo("25%");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCurrency_whenFormatNumber_currencyWithChosenLocalisation_thenGetExpectedResult() {
|
||||||
|
double value = 23_500;
|
||||||
|
assertThat(currencyWithChosenLocalisation(value, new Locale("en", "US"))).isEqualTo("$23,500.00");
|
||||||
|
assertThat(currencyWithChosenLocalisation(value, new Locale("zh", "CN"))).isEqualTo("¥23,500.00");
|
||||||
|
assertThat(currencyWithChosenLocalisation(value, new Locale("pl", "PL"))).isEqualTo("23 500 zł");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCurrency_whenFormatNumber_currencyWithDefaultLocalisation_thenGetExpectedResult() {
|
||||||
|
double value = 23_500;
|
||||||
|
assertThat(currencyWithDefaultLocalisation(value)).isEqualTo("£23,500.00");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue