NumberFormat class in Java
NumberFormat可帮助您格式化和解析任何语言环境的数字。它是所有数字格式的抽象基类。
以下是NumberFormat类的一些方法-
| 修饰符和类型 th> | 方法和说明 th> |
|---|---|
| Object | clone()覆盖Cloneable。 | boolean | equals(Object obj)重写equals。 |
| String。 | format(double number)Specialization |
| 抽象StringBuffer | format(双精度数字,StringBuffer到AppendTo,FieldPosition pos)格式的专业化。 | String | format(长整数)格式的专业化。 |
| 抽象StringBuffer | format(长整数, StringBuffer toAppendTo,FieldPosition pos)格式的专业化。 |
例
现在让我们看一个实现NumberFormat类的示例-
现场演示
1 2 3 4 5 6 7 8 9 10 11 | import java.text.NumberFormat; import java.util.Locale; public class Demo { public static void main(String[] args) { NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE); double points = 2.15; double totalPoints = points * 1000; System.out.println(n.format(points)); System.out.println(n.format(totalPoints)); } } |
输出量
这将产生以下输出-
1 2 | 2,15 € 2 150,00 € |
例
现在让我们来看另一个例子-
现场演示
1 2 3 4 5 6 7 8 9 10 11 12 | import java.text.NumberFormat; import java.util.Locale; public class Demo { public static void main(String[] args) { Locale enLocale = new Locale("en","US"); Locale daLocale = new Locale("da","DK"); NumberFormat numberFormat = NumberFormat.getInstance(daLocale); System.out.println(numberFormat.format(100.76)); numberFormat = NumberFormat.getInstance(enLocale); System.out.println(numberFormat.format(100.76)); } } |
输出量
这将产生以下输出-
1 2 | 100,76 100.76 |