[Java] In some format in Java – Formatting Numeric Print Output in Java
In Java offers 2 method in the format we are printf and format and 2 This method has similar functions.
With the 1 variables can be formatted in different ways it with. CEO
System.out.format("The value of " + "the float variable is " + "%f, while the value of the " + "integer variable is %d, " + "and the string is %s", floatVar, intVar, stringVar);
int i = 461012; System.out.format("The value of i is: %d%n", i);
Another thing is that you can print format according to the French system, separating the integer part and the decimal part of the real number in decimal format Locale.FRANCE. CEO:
System.out.format(Locale.FRANCE, "The value of the float " + "variable is %f, while the " + "value of the integer variable " + "is %d, and the string is %s%n", floatVar, intVar, stringVar);
I have a table format:
And you can run a simple program:
import java.util.Calendar; import java.util.Locale; public class FormattingNumericPrintOutput { public static void main(String[] args) { long n = 461012; System.out.format("%d%n", n); // --> "461012" System.out.format("%08d%n", n); // --> "00461012" System.out.format("%+8d%n", n); // --> " +461012" System.out.format("%,8d%n", n); // --> " 461,012" System.out.format("%+,8d%n%n", n); // --> "+461,012" double pi = Math.PI; System.out.format("%f%n", pi); // --> "3.141593" System.out.format("%.3f%n", pi); // --> "3.142" System.out.format("%10.3f%n", pi); // --> " 3.142" System.out.format("%-10.3f%n", pi); // --> "3.142" System.out.format(Locale.FRANCE, "%-10.4f%n%n", pi); // --> "3,1416" //in theo hệ thống Pháp Calendar c = Calendar.getInstance(); //lấy thời gian hiện hành System.out.format("%tB %te, %tY%n ", c, c, c); // --> "July 20, 2013" System.out.format("%tl:%tM %tp%n", c, c, c); // --> " 2:51 pm" System.out.format("%tD%n", c); // --> "07/20/13" } }
If the format of double data type as according to his à float?