📄 numberformatter.java
字号:
package examples.i18n;
import java.text.NumberFormat;
import java.util.Locale;
/** Class to demonstrate the use of the NumberFormat
* class for currency and other numbers
*/
public class NumberFormatter {
/** Test program for the class
* @param args not used
*/
public static void main( String[] args ) {
double x = 100000000.12345;
NumberFormat nfDefault
= NumberFormat.getInstance();
NumberFormat nfGermany
= NumberFormat.getInstance( Locale.GERMANY );
test( "Default number format",
nfDefault, nfGermany, x );
nfDefault.setMaximumFractionDigits( 6 );
nfGermany.setMaximumFractionDigits( 6 );
test( "Default number format, more precision",
nfDefault, nfGermany, x );
double y = 0.12345;
NumberFormat nfDefaultPc
= NumberFormat.getPercentInstance();
NumberFormat nfGermanyPc
= NumberFormat.getPercentInstance(
Locale.GERMANY );
test( "Percent format",
nfDefaultPc, nfGermanyPc, y );
double z = 12345.6789;
NumberFormat nfDefaultCr
= NumberFormat.getCurrencyInstance();
NumberFormat nfGermanyCr
= NumberFormat.getCurrencyInstance(
Locale.GERMANY );
test( "Currency format",
nfDefaultCr, nfGermanyCr, z );
}
/** Test method
* @param label the name of the test
* @param defNf the NumberFormat object for the
* default locale
* @param gerNf the NumberFormat object for the
* GERMANY locale
* @param value the value to be formatted.
*/
public static void test( String label,
NumberFormat defNf,
NumberFormat gerNf,
double value ) {
System.out.println( label );
System.out.println( "\tdefault locale: "
+ defNf.format( value ) );
System.out.println( "\tGerman locale: "
+ gerNf.format( value ) );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -