Java Tutorial/I18N/DecimalFormat
Содержание
- 1 Arabic Digits
- 2 Displaying numbers with commas
- 3 Display numbers in scientific notation
- 4 Format a number with leading zeros
- 5 new DecimalFormat("##00")
- 6 new DecimalFormat("0.00")
- 7 new DecimalFormat("#.), append zero
- 8 new DecimalFormat("#,###,###"), group
- 9 new DecimalFormat(".######"), keep fraction
- 10 new DecimalFormat("#.######") more digits
- 11 new DecimalFormat("#.#") two digits
- 12 The 0 symbol shows a digit or 0 if no digit present
- 13 The . symbol indicates the decimal point
- 14 The , symbol is used to group numbers
- 15 The " symbol is used to quote literal symbols
- 16 The ; symbol is used to specify an alternate pattern for negative values
- 17 Use group separators and show trailing zeros
Arabic Digits
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ArabicDigitsI18N extends JPanel {
public ArabicDigitsI18N() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
dfs.setZeroDigit("\u0660");
df.setDecimalFormatSymbols(dfs);
JLabel label = new JLabel(df.format(1234567.89));
label.setFont(new Font("Lucida Sans", Font.PLAIN, 22));
add(label);
}
public static void main(String[] argv) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", new ArabicDigitsI18N());
frame.pack();
frame.setVisible(true);
}
}
Displaying numbers with commas
import java.text.DecimalFormat;
public class Main {
public static void main(String args[]) {
double d = 123456.78;
DecimalFormat df = new DecimalFormat("#####0.00");
System.out.println(df.format(d));
}
}
Display numbers in scientific notation
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String args[]) {
NumberFormat formatter = new DecimalFormat();
int maxinteger = Integer.MAX_VALUE;
System.out.println(maxinteger);
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(maxinteger));
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(maxinteger));
int mininteger = Integer.MIN_VALUE;
System.out.println(mininteger);
formatter = new DecimalFormat("0.######E0");
System.out.println(formatter.format(mininteger));
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(mininteger));
double d = 0.12345;
formatter = new DecimalFormat("0.#####E0");
System.out.println(formatter.format(d));
formatter = new DecimalFormat("000000E0");
System.out.println(formatter.format(d));
}
}
Format a number with leading zeros
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat("0000000");
String number = formatter.format(2500);
System.out.println("Number with lading zeros: " + number);
}
}
new DecimalFormat("##00")
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("##00");
String s = formatter.format(0); // 00
System.out.println(s);
}
}
new DecimalFormat("0.00")
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("0.00");
String s = formatter.format(-.567); // -0.57
System.out.println(s);
}
}
new DecimalFormat("#.), append zero
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("#.000000");
String s = formatter.format(-1234.567);
System.out.println(s);
}
}
//-1234.567000
new DecimalFormat("#,###,###"), group
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("#,###,###");
String s = formatter.format(-1234.567);
System.out.println(s);
s = formatter.format(-1234567.890);
System.out.println(s);
}
}
// -1,235
// -1,234,568
new DecimalFormat(".######"), keep fraction
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat(".######");
String s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
}
}//-1234.567
new DecimalFormat("#.######") more digits
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("#.######");
String s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
}
}
new DecimalFormat("#.#") two digits
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("#.#");
String s = formatter.format(-1234.567); // -1234.6
System.out.println(s);
}
}
The 0 symbol shows a digit or 0 if no digit present
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("000000");
String s = formatter.format(-1234.567);
System.out.println(s);
// number was rounded up
}
}
//-001235
The . symbol indicates the decimal point
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat(".00");
String s = formatter.format(-.567); // -.57
System.out.println(s);
}
}
The , symbol is used to group numbers
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat("#,###,###");
String s = formatter.format(-1234.567);
System.out.println(s);
s = formatter.format(-1234567.890);
System.out.println(s);
}
}
// -1,235
// -1,234,568
The " symbol is used to quote literal symbols
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Main {
public static void main(String[] argv) throws Exception {
NumberFormat formatter = new DecimalFormat(""#"#");
String s = formatter.format(-1234.567);
System.out.println(s);
}
}
//-#1235
The ; symbol is used to specify an alternate pattern for negative values
public class Main{
public static void main(String[] argv) throws Exception{
NumberFormat formatter = new DecimalFormat("#;(#)");
String s = formatter.format(-1234.567); // (1235)
sysout
}}
Use group separators and show trailing zeros
import java.text.DecimalFormat;
public class Main {
public static void main(String[] argv) throws Exception {
DecimalFormat df = new DecimalFormat("#,.00;(#,.00)");
System.out.println(df.format(7123.00));
}
}