Java by API/java.text/NumberFormat
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 NumberFormat: format(Object obj)
- 2 NumberFormat.getCurrencyInstance();
- 3 NumberFormat: getCurrencyInstance(Locale.UK)
- 4 NumberFormat: getInstance()
- 5 NumberFormat: getIntegerInstance(Locale.ITALIAN)
- 6 NumberFormat: getNumberInstance()
- 7 NumberFormat: getNumberInstance(Locale.FRENCH)
- 8 NumberFormat: getNumberInstance(Locale inLocale)
- 9 NumberFormat: getPercentInstance(Locale inLocale)
- 10 NumberFormat: parse(String source)
- 11 NumberFormat.setMaximumFractionDigits(int newValue)
- 12 NumberFormat: setMinimumFractionDigits(int newValue)
- 13 NumberFormat: setMinimumIntegerDigits(int newValue)
- 14 NumberFormat: setParseIntegerOnly(boolean value)
NumberFormat: format(Object obj)
/*
4096.251 parses as 4096.251 and formats as 4,096.251
*/
import java.text.NumberFormat;
import java.text.ParseException;
public class MainClass {
public static void main(String[] av) {
String input = "4096.251";
NumberFormat defForm = NumberFormat.getInstance();
try {
Number d = defForm.parse(input);
System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
} catch (ParseException pe) {
System.err.println(input + "not parseable!");
}
}
}
NumberFormat.getCurrencyInstance();
import java.text.NumberFormat;
public class Mortgage {
public static void main(String[] args) {
double payment = Math.random() * 1000;
System.out.println("Your payment is ");
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(payment));
}
}
NumberFormat: getCurrencyInstance(Locale.UK)
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.Format;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("Number Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("SansSerif", Font.BOLD, 16);
JLabel label;
JFormattedTextField input;
JPanel panel;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
Format currency = NumberFormat.getCurrencyInstance(Locale.UK);
label = new JLabel("UK Currency:");
input = new JFormattedTextField(currency);
input.setValue(2424.50);
input.setColumns(20);
input.setFont(font);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
NumberFormat: getInstance()
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.Format;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("Number Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("SansSerif", Font.BOLD, 16);
JLabel label;
JFormattedTextField input;
JPanel panel;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
Format general = NumberFormat.getInstance();
label = new JLabel("General/Instance:");
input = new JFormattedTextField(general);
input.setValue(2424.50);
input.setColumns(20);
input.setFont(font);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
NumberFormat: getIntegerInstance(Locale.ITALIAN)
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.Format;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("Number Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("SansSerif", Font.BOLD, 16);
JLabel label;
JFormattedTextField input;
JPanel panel;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
Format integer = NumberFormat.getIntegerInstance(Locale.ITALIAN);
label = new JLabel("Italian integer:");
input = new JFormattedTextField(integer);
input.setValue(2424.50);
input.setColumns(20);
input.setFont(font);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
NumberFormat: getNumberInstance()
/*Output:
* User"s number (DEFAULT LOCALE): 1,976.083
* */
import java.text.NumberFormat;
public class MainClass {
public static void main(String args[]) throws Exception {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(false);
double usersNumber = 1976.0826;
numberFormat = NumberFormat.getNumberInstance();
System.out.println("User"s number (DEFAULT LOCALE): " + numberFormat.format(usersNumber));
}
}
NumberFormat: getNumberInstance(Locale.FRENCH)
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.Format;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainClass {
public static void main(String args[]) throws Exception {
JFrame frame = new JFrame("Number Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("SansSerif", Font.BOLD, 16);
JLabel label;
JFormattedTextField input;
JPanel panel;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
Format number = NumberFormat.getNumberInstance(Locale.FRENCH);
label = new JLabel("French Number:");
input = new JFormattedTextField(number);
input.setValue(2424.50);
input.setColumns(20);
input.setFont(font);
panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel.add(label);
panel.add(input);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
NumberFormat: getNumberInstance(Locale inLocale)
/*Output:
* User"s number (US): 1,976.083
* */
import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
public static void main(String args[]) throws Exception {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(false);
double usersNumber = 1976.0826;
numberFormat = NumberFormat.getNumberInstance(Locale.US);
System.out.println("User"s number (US): " + numberFormat.format(usersNumber));
}
}
NumberFormat: getPercentInstance(Locale inLocale)
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");
}
}
}
NumberFormat: parse(String source)
/*
4096.251 parses as 4096.251 and formats as 4,096.251
*/
import java.text.NumberFormat;
import java.text.ParseException;
public class MainClass {
public static void main(String[] av) {
String input = "4096.251";
NumberFormat defForm = NumberFormat.getInstance();
try {
Number d = defForm.parse(input);
System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
} catch (ParseException pe) {
System.err.println(input + "not parseable!");
}
}
}
NumberFormat.setMaximumFractionDigits(int newValue)
/*
0.0 formats as 000.00
1.0 formats as 001.00
3.142857142857143 formats as 003.1429
100.2345678 formats as 100.2346
*/
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] av) {
double data[] = { 0, 1, 22d / 7, 100.2345678 };
NumberFormat form = NumberFormat.getInstance();
// Set it to look like 999.99[99]
form.setMinimumIntegerDigits(3);
form.setMinimumFractionDigits(2);
form.setMaximumFractionDigits(4);
// Now print using it.
for (int i = 0; i < data.length; i++)
System.out.println(data[i] + "\tformats as " + form.format(data[i]));
}
}
NumberFormat: setMinimumFractionDigits(int newValue)
/*
0.0 formats as 000.00
1.0 formats as 001.00
3.142857142857143 formats as 003.1429
100.2345678 formats as 100.2346
*/
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] av) {
double data[] = { 0, 1, 22d / 7, 100.2345678 };
NumberFormat form = NumberFormat.getInstance();
// Set it to look like 999.99[99]
form.setMinimumIntegerDigits(3);
form.setMinimumFractionDigits(2);
form.setMaximumFractionDigits(4);
// Now print using it.
for (int i = 0; i < data.length; i++)
System.out.println(data[i] + "\tformats as " + form.format(data[i]));
}
}
NumberFormat: setMinimumIntegerDigits(int newValue)
/*
0.0 formats as 000.00
1.0 formats as 001.00
3.142857142857143 formats as 003.1429
100.2345678 formats as 100.2346
*/
import java.text.NumberFormat;
public class MainClass {
public static void main(String[] av) {
double data[] = { 0, 1, 22d / 7, 100.2345678 };
NumberFormat form = NumberFormat.getInstance();
// Set it to look like 999.99[99]
form.setMinimumIntegerDigits(3);
form.setMinimumFractionDigits(2);
form.setMaximumFractionDigits(4);
// Now print using it.
for (int i = 0; i < data.length; i++)
System.out.println(data[i] + "\tformats as " + form.format(data[i]));
}
}
NumberFormat: setParseIntegerOnly(boolean value)
/*Output:
* User"s number (US): 1,976.083
* */
import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
public static void main(String args[]) throws Exception {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(false);
double usersNumber = 1976.0826;
numberFormat = NumberFormat.getNumberInstance(Locale.US);
System.out.println("User"s number (US): " + numberFormat.format(usersNumber));
}
}