Java Tutorial/I18N/NumberFormat
Содержание
- 1 Format a number for a locale
- 2 Format a number with DecimalFormat
- 3 Format for GERMAN locale
- 4 Format for the default locale
- 5 Formatting and Parsing a Locale-Specific Percentage
- 6 Formatting and Parsing Locale-Specific Currency
- 7 Formatting a Number in Exponential Notation
- 8 Parse a GERMAN number
- 9 Parse a number for a locale
- 10 Parse a number with NumberFormat and Locale.CANADA
- 11 Parse number with NumberFormat and Locale
- 12 Set format to two decimal places
- 13 Use grouping to display a number
- 14 Use java.text.NumberFormat to format a currency value.
- 15 Using only 0"s to the left of E forces no decimal point
Format a number for a locale
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] args) throws Exception { NumberFormat formatter = NumberFormat.getNumberInstance(Locale.ITALY); String number = formatter.format(123456789.12); System.out.println("Number in Italy: " + number); formatter = NumberFormat.getNumberInstance(Locale.JAPAN); number = formatter.format(123456789.12); System.out.println("Number in Japan: " + number); }
}</source>
Format a number with DecimalFormat
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] args) { double money = 123456789.12; NumberFormat formatter = new DecimalFormat("#0.00"); System.out.println(money); System.out.println(formatter.format(money)); }
}</source>
Format for GERMAN locale
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { Locale locale = Locale.GERMAN; String string = NumberFormat.getNumberInstance(locale).format(-1234.56); System.out.println(string); }
} //-1.234,56</source>
Format for the default locale
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { String string = NumberFormat.getNumberInstance().format(-1234.56); System.out.println(string); }
} //-1,234.56</source>
Formatting and Parsing a Locale-Specific Percentage
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { // Format Locale locale = Locale.CANADA; String string = NumberFormat.getPercentInstance(locale).format(123.45); }
} // 12,345%</source>
Formatting and Parsing Locale-Specific Currency
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { // Format Locale locale = Locale.GERMANY; String string = NumberFormat.getCurrencyInstance(locale).format(123.45); }
} // 123,45 DM</source>
Formatting a Number in Exponential Notation
<source lang="java">
The "E" symbol specifies that a number should be formatted in exponential notation. E symbol separates the mantissa from the exponent. E symbol must be followed by one or more "0" symbols. The number of "0" symbols specifies the minimum number of digits used to display the exponent.</source>
Parse a GERMAN number
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { Number number = NumberFormat.getNumberInstance(Locale.GERMAN).parse("-1.234,56"); if (number instanceof Long) { System.out.println("Long value"); } else { System.out.println("Double value"); } }
}</source>
Parse a number for a locale
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] args) throws Exception { Number number = NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75"); if (number instanceof Long) { System.out.println("Long value: " + number.longValue()); } else if (number instanceof Double) { System.out.println("Double value: " + number.doubleValue()); } }
}</source>
Parse a number with NumberFormat and Locale.CANADA
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { Number number = NumberFormat.getPercentInstance(Locale.CANADA).parse("123.45%"); // 1.2345 if (number instanceof Long) { System.out.println("Long value"); } else { System.out.println("Double value"); } }
}</source>
Parse number with NumberFormat and Locale
<source lang="java">
import java.text.NumberFormat; import java.util.Locale; public class Main {
public static void main(String[] argv) throws Exception { Number number = NumberFormat.getCurrencyInstance(Locale.GERMANY).parse("$123.45"); if (number instanceof Long) { System.out.println("Long value"); } else { System.out.println("Double value"); } }
}</source>
Set format to two decimal places
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String args[]) { NumberFormat nf = NumberFormat.getInstance(); System.out.println("Default format: " + nf.format(1234567.678)); nf.setMaximumFractionDigits(2); System.out.println("Format with two decimal places: " + nf.format(1234567.678)); }
} /* Default format: 1,234,567.678 Format with two decimal places: 1,234,567.68
- /</source>
Use grouping to display a number
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String args[]) { NumberFormat nf = NumberFormat.getInstance(); System.out.println("Default format: " + nf.format(1234567.678)); nf.setGroupingUsed(false); System.out.println("Format without groupings: " + nf.format(1234567.678)); }
} /* Default format: 1,234,567.678 Format without groupings: 1234567.678
- /</source>
Use java.text.NumberFormat to format a currency value.
<source lang="java">
import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat nf = NumberFormat.getCurrencyInstance(); System.out.println("currency format: " + nf.format(1234.56) + " " + nf.format(-1234.56)); }
} //currency format: $1,234.56 -$1,234.56</source>
Using only 0"s to the left of E forces no decimal point
<source lang="java">
import java.text.DecimalFormat; import java.text.NumberFormat; public class Main {
public static void main(String[] argv) throws Exception { NumberFormat formatter = new DecimalFormat("0E0"); String s = formatter.format(-1234.567); System.out.println(s); }
} // -1E3</source>