Hello! You need to format the BigDecimal number as follows:

  1. 12 300 formatted in 12.3 thousand
  2. 12.3 million format in 12.3 million

And so on. Is there any effective way to do this using java / kotlin?

PS: I simply substitute the ending thousand or million, depending on the number to be formatted. PSS: I will use this formatting in the Android project.

    1 answer 1

    public class Formatter { private static final BigDecimal HUNDRED = new BigDecimal("100"); private static final BigDecimal THOUSAND = new BigDecimal("1000"); private static final BigDecimal MILLION = new BigDecimal("1000000"); private static final BigDecimal BILLION = new BigDecimal("1000000000"); public static String format(BigDecimal value) { if (value.compareTo(BILLION) > 0) { return String.format("%.1f млрд.", value.divide(BILLION)); } else if (value.compareTo(MILLION) > 0) { return String.format("%.1f млн.", value.divide(MILLION)); } else if (value.compareTo(THOUSAND) > 0) { return String.format("%.1f тыс.", value.divide(THOUSAND)); } else if (value.compareTo(HUNDRED) > 0) { return String.format("%.1f сот.", value.divide(HUNDRED)); } else { return String.format("%.1f", value); } } }