Java Tutorial/Data Type/Decimal

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

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





DecimalFormat("0E0")

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

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

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 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;
    formatter = new DecimalFormat("0.######E0");
    System.out.println(formatter.format(maxinteger));
  }
}
//2.147484E9





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





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("0")

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(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





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





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





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