Java/Data Type/Decimal

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Change the decimal separator is set to "."

 
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
public class Main {
  public static void main(String args[]) {
    double d = 123456.7890;
    DecimalFormat df = new DecimalFormat("#####0.00");
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    dfs.setDecimalSeparator(".");
    df.setDecimalFormatSymbols(dfs);
    System.out.println(df.format(d));
  }
}





Convert a number using the current Locale()

 
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
  public static void main(String[] args) throws Exception {
   System.out.println(NumberFormat.getInstance(new Locale("us")).format(12345.12345)); 
  }
}
//12,345.123





DecimalFormat("0000000000E0")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("0000000000E0");
    String s = formatter.format(-1234.567); // -1234567000E-6
    System.out.println(s);
  }
}





DecimalFormat("00.00E0")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("00.00E0");
    String s = formatter.format(-1234.567); // -12.35E2
    System.out.println(s);
    s = formatter.format(-.1234567); // -12.35E-2
    System.out.println(s);
  }
}





DecimalFormat("000E00")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("000E00");
    String s = formatter.format(-1234.567); // -123E01
    System.out.println(s);
  }
}





DecimalFormat("00E00")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("00E00");
    String s = formatter.format(-1234.567);
    System.out.println(s);
  }
}
// -12E02





DecimalFormat("##E0") (exponent must be multiple of 2)

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("##E0"); 
    String s = formatter.format(-1234.567);
    System.out.println(s);
    s = formatter.format(-123.4567);
    System.out.println(s);
    s = formatter.format(-12.34567);
    System.out.println(s);
  }
}
// -12E2
// -1.2E2
// -12E0





DecimalFormat("###E0") (exponent must be multiple of 3)

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) {
    DecimalFormat formatter = new DecimalFormat("###E0"); 
    String s = formatter.format(-1234.567); // -1.23E3
    System.out.println(s);
    s = formatter.format(-123.4567); // -123E0
    System.out.println(s);
    s = formatter.format(-12.34567); // -12.3E0
    System.out.println(s);
    s = formatter.format(-1.234567); // -12.3E0
    System.out.println(s);
    s = formatter.format(-.1234567); // -123E-3
    System.out.println(s);
  }
}





Display a currency value

 

import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat df = new DecimalFormat("\u00a4#,##0.00");
    System.out.println(df.format(4232.19));
    System.out.println(df.format(-4232.19));
  }
}





Display a percentage

 

import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat df = new DecimalFormat("#%");
    System.out.println(df.format(0.19));
    System.out.println(df.format(-0.19));
  }
}
/*19%
-19%
*/





Display numbers with commas

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {
    double d = 123456.7890;
    DecimalFormat df = new DecimalFormat("#####0.00");
    System.out.println(df.format(d));
  }
}
//123456.79





Display numbers with leading zeroes

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {
    DecimalFormat df = new DecimalFormat("0000000000000000");
    String z = df.format(123456);
    System.out.println(z);
  }
}





Display the amount of free memory in the Java Virtual Machine.

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");
    long freeMem = Runtime.getRuntime().freeMemory();
    System.out.println(df.format(freeMem / 1000000F) + " MB");
  }
}





Display the maximum amount of memory

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");
    long maxMem = Runtime.getRuntime().maxMemory();
    System.out.println(df.format(maxMem / 1000000F) + " MB");
  }
}





Display the total amount of memory in the Java virtual machine.

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] args) {
    DecimalFormat df = new DecimalFormat("0.00");
    long totalMem = Runtime.getRuntime().totalMemory();
    System.out.println(df.format(totalMem / 1000000F) + " MB");
  }
}





Force minimum number of digits to left and right of decimal point

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("0.0E0");
    String s = formatter.format(-1234.567); // -1.2E3
    System.out.println(s);
  }
}





new DecimalFormat("#.#")

 
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);
  }
}





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("#.000000")

 
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("000000E0")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {
    System.out.println(new DecimalFormat("000000E0").format(123123));
  }
}
//123123E0





new DecimalFormat("0.######E0")

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {
    System.out.println(new DecimalFormat("0.######E0").format(12345));
  }
}
//1.2345E4





new DecimalFormat("#.######") (1)

 
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(".######") (2)

 
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(abc#)

 
import java.text.DecimalFormat;
import java.text.Format;
public class Main {
  public static void main(String[] argv) throws Exception {
    Format formatter = new DecimalFormat(""abc"#");
    String s = formatter.format(-1234.567);
    System.out.println(s);
  }
}
// -abc1235





new DecimalFormat("#,###,###") (grouping)

 
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





Round number to fewer decimals

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] args) {
    double numberToRound = 12345.6789;
    DecimalFormat df = new DecimalFormat("0.000");
    System.out.println("Rounded number = " + df.format(numberToRound));
    System.out.println(String.format("Rounded number = %.3f", numberToRound));
  }
}
/*
Rounded number = 12345.679
Rounded number = 12345.679
*/





Set format to two decimal places: set Maximum Fraction Digits

 
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
*/





Set format to two decimal places: set Minimum Fraction Digits

 
import java.text.NumberFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(2);
    System.out.println("Format with two decimal places: " + nf.format(1234567.678));
  }
}





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 number of #"s to the left of the decimal point sets the multiple of the exponent.

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    DecimalFormat formatter = new DecimalFormat("#E0"); // exponent can be any
                                                        // value
    String s = formatter.format(-1234.567); 
    System.out.println(s);
    
    s = formatter.format(-.1234567); 
    System.out.println(s);
  }
}
//-.1E4
//-.1E0





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 zeroes

 
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));
  }
}





Use java.text.DecimalFormat to format integer

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {    
    System.out.println(new DecimalFormat("0.#####E0").format(123456));
  }
}
//1.23456E5





Use new DecimalFormat("0.#####E0") to format double

 
import java.text.DecimalFormat;
public class Main {
  public static void main(String args[]) {
    double d = 0.12345;
    System.out.println(new DecimalFormat("0.#####E0").format(d));
  }
}
//1.2345E-1