Java by API/java.text/DecimalFormat

Материал из Java эксперт
Перейти к: навигация, поиск

DecimalFormat: applyPattern(String pattern)

   <source lang="java">

import java.text.DecimalFormat; import java.text.NumberFormat; public class MainClass {

 public static void main(String[] argv) {
   double d = 1234567.89;
   double n = -1234567.89;
   String pattern = "#,###.##;(#,###.##)";
   NumberFormat nf = NumberFormat.getInstance();
   if (nf instanceof DecimalFormat) {
     DecimalFormat df = (DecimalFormat) nf;
     df.applyPattern(pattern);
     System.out.println(df.format(d));
     System.out.println(df.format(n));
   }
 }

}


 </source>
   
  
 
  



DecimalFormat: getDecimalFormatSymbols()

   <source lang="java">

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 Main extends JPanel {

 public Main() {
   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 Main());
   frame.pack();
   frame.setVisible(true);
 }

}

 </source>
   
  
 
  



DecimalFormat: setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)

   <source lang="java">

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 MainClass extends JPanel {

 public MainClass() {
   NumberFormat nf = NumberFormat.getInstance();
   if (nf instanceof DecimalFormat) {
     DecimalFormat df = (DecimalFormat) nf;
     DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
     // set the beginning of the range to Arabic digits
     dfs.setZeroDigit("\u0660");
     df.setDecimalFormatSymbols(dfs);
   }
   JLabel label = new JLabel(nf.format(1234567.89));
   label.setFont(new Font("Lucida Sans", Font.PLAIN, 22));
   add(label);
 }
 public static void main(String[] argv) {
   MainClass panel = new MainClass();
   JFrame frame = new JFrame("Arabic Digits");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().add("Center", panel);
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>