New feature of jdk12: compactnumberformat

New feature of jdk12: compactnumberformat

brief introduction

Jdk12 introduces a new class for formatting numbers called compactnumberformat. It is mainly convenient for us to abbreviate long numbers. For example, 1000 can be abbreviated as 1K or 1 thousand.

This article will explain the basic composition and usage of compactnumberformat. Finally, the article ends with a practical example.

Compactnumberformat is a subclass of numberformat as part of formatting numbers. The function is to format numbers. The easiest way to build a compactnumberformat is to use numberformat Getcompactnumberinstance method.

The following is the definition of this method:

    public static NumberFormat getCompactNumberInstance(Locale locale,NumberFormat.Style formatStyle)

Method needs to pass in two parameters: locale and style.

Locale

Locale represents local language features. For example, in US locale, 10000 can be expressed as "10K", while in China locale, 10000 becomes "10000".

Style

There are two types of styles, short and long. For example, the short of 10000 means "10K", while its long means "10 thousand".

JDK has customized many built-in compact implementations for us. We can use them directly:

@Test
    public void testCompactNumberFormat(){
        NumberFormat fmtShort = NumberFormat.getCompactNumberInstance(
                Locale.US,NumberFormat.Style.SHORT);

        NumberFormat fmtLong = NumberFormat.getCompactNumberInstance(
                Locale.US,NumberFormat.Style.LONG);

        log.info(fmtShort.format(312));
        log.info(fmtShort.format(3123));
        log.info(fmtShort.format(31234));

        log.info(fmtLong.format(312));
        log.info(fmtLong.format(3123));
        log.info(fmtLong.format(31234));
    }

Output results:

 312
 3K
 31K

 312
 3 thousand
 31 thousand

Custom compactnumberformat

In addition to using the numberformat tool class, we can also customize compactnumberformat.

Let's first look at the definition of compactnumberformat:

public CompactNumberFormat(String decimalPattern,DecimalFormatSymbols symbols,String[] compactPatterns)
public CompactNumberFormat(String decimalPattern,String[] compactPatterns,String pluralRules)

Compactnumberformat can accept constructors with 3 or 4 parameters.

Decimalpatterns and symbols are used to parse numbers normally, and compactpatterns are used to generate abbreviations. Pluralrules are plural rules.

@Test
    public void useCustom(){
         String[] compactPatterns
                = {"","","0千","0万","00万","0百万","0千万","0亿","00亿","0百亿","0千亿","0兆","00兆","000兆"};

         DecimalFormat decimalFormat = (DecimalFormat)
                NumberFormat.getNumberInstance(Locale.CHINA);

         CompactNumberFormat format
                = new CompactNumberFormat( decimalFormat.toPattern(),decimalFormat.getDecimalFormatSymbols(),compactPatterns);

        log.info(format.format(312340000));
    }

The above is a custom abbreviation rule.

Output results:

3亿

Resolve compactnumber

It can be generated and parsed naturally. Let's take an example of parsing:

    @Test
    public void testParse() throws ParseException {
        NumberFormat fmtLong = NumberFormat.getCompactNumberInstance(
                Locale.US,NumberFormat.Style.LONG);
        log.info(String.valueOf(fmtLong.parse("3 thousand")));
    }

Output results:

3000
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>