📜  Java内部化-格式化模式

📅  最后修改于: 2020-11-15 03:38:16             🧑  作者: Mango


以下是格式化模式中字符的使用。

Sr.No. Class & Description
1

0

To display 0 if less digits are present.

2

#

To display digit ommitting leading zeroes.

3

.

Decimal separator.

4

,

Grouping separator.

5

E

Mantissa and Exponent separator for exponential formats.

6

;

Format separator.

7

Negative number prefix.

8

%

Shows number as percentage after multiplying with 100.

9

?

Shows number as mille after multiplying with 1000.

10

X

To mark character as number prefix/suffix.

11

To mark quote around special characters.

在此示例中,我们将根据不同的格式设置数字格式。

IOTester.java

import java.text.DecimalFormat;

public class I18NTester {
   public static void main(String[] args) {
      String pattern = "###.###";
      double number = 123456789.123;

      DecimalFormat numberFormat = new DecimalFormat(pattern);

      System.out.println(number);

      //pattern ###.###
      System.out.println(numberFormat.format(number));

      //pattern ###.#
      numberFormat.applyPattern("###.#");
      System.out.println(numberFormat.format(number));

      //pattern ###,###.##
      numberFormat.applyPattern("###,###.##");
      System.out.println(numberFormat.format(number));

      number = 9.34;

      //pattern 000.###
      numberFormat.applyPattern("000.##");
      System.out.println(numberFormat.format(number));           
   }
}

输出

它将打印以下结果。

1.23456789123E8
1,2345,6789.12

打印