Home > Article > Computer Tutorials > Simple Java programming illustrated
printf("s=%.3f",s); keep 3 decimal places
Look at the following examples:
double d = 345.678;
String s = "hello!";
int i = 1234;
//"%" means formatted output, and the content after "%" is the definition of the format.
System.out.printf("%f",d);//"f" means formatted output floating point number.
System.out.printf("%9.2f",d);//The 9 in "9.2" represents the length of the output, and 2 represents the number of digits after the decimal point.
System.out.printf("% 9.2f",d); //""" indicates that the output number has a positive or negative sign.
System.out.printf("%-9.4f",d); //"-" means the output number is left-aligned (default is right-aligned).
System.out.printf("% -9.3f",d); //"-" indicates that the output number is signed and left-aligned.
System.out.printf("%d",i);//"d" means output decimal integer.
System.out.printf("%o",i);//"o" means outputting an octal integer.
System.out.printf("%x",i);//"d" means outputting a hexadecimal integer.
System.out.printf("%#x",i);//"d" means outputting an integer with hexadecimal flag.
System.out.printf("%s",s);//"d" represents the output string.
System.out.printf ("Output a floating point number: %f, an integer: %d, a string: %s", d, i, s); // Multiple variables can be output, pay attention to the order.
System.out.printf ("String: %2$s, hexadecimal number of %1$d: %1$#x", i, s); // "X$" represents the number variables.
just for reference:
/*
*It is known that the volume of the sphere is 4/3πr3. Try writing a program that inputs the radius of the sphere and calculates the volume of the sphere.
*Enter
from the command window*/
import java.util.Scanner;
public class Volume{ //Define class member variables in Java
public static void main(String args[]){ //main() method body, the entrance to a java application program
Scanner s=new Scanner(System.in);
System.out.println ("Please enter the radius r of the sphere you want to volume"); //Output prompt content
double r=s.nextDouble(); //Define a double variable
double v; //Define a double variable
v=4*Math.PI*Math.pow(r,3)/3; //Operation expression
System.out.println("The volume of the sphere is "v"); //Output the calculation results and output "The volume of the sphere is" at the beginning
}
}
It is very convenient to use the classes that come with Java. Let me give you an example:
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args) {
double num = 0.235463;
DecimalFormat fmt = new DecimalFormat(".
%"");System.out.println(fmt.format(num));
}
}
// Display: 23.546%
The # in ".
How to set the number of decimal points to retain in Java calculations
method one:
rounding
double f = 111231.5585;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
Retain two decimal places
Method 2:
java.text.DecimalFormat df =new java.text.DecimalFormat(”#.00″);
df.format (the number you want to format);
Example: new java.text.DecimalFormat(”#.00″).format(3.1415926)
#.00 means two decimal places #.0000 means four decimal places and so on...
Method 3:
double d = 3.1415926;
String result = String .format(”%.2f”);
###%.2f %. means any number of digits before the decimal point. 2 means two decimal places. The formatted result is f, which means floating point type. ###The above is the detailed content of Simple Java programming illustrated. For more information, please follow other related articles on the PHP Chinese website!