/**
* Java two integers divide two decimal places and convert decimals to percentages
* In Java, when two integers are divided, because the number after the decimal point is truncated, the result of the operation will be an integer, if you want to get the result of the operation as a floating point, you must convert both integers one or both to a floating point.
For example:
(float) A/b///convert one of the integers to a floating-point number and divide it with another integer
A/(float) b
(float) A/(float) b//convert two integers to floating-point numbers at the same time and then divide
*/
//////// Calculate the number of decimal places:
System.out.println ("1/8 =" + 1 / (float) 8);
float TT = Math.round (1 / (float) 8); // The return type is int.
System.out.println ("rounded decimals:" + TT);
System.out.println ("Rounding to decimal 2:" + Math.round (2.1546));
////method 1
double dd = (double) (Math.round (1 / (float) 8 * 100) /100.0);
System.out.println ("dd =" + dd);
// (double) (Math.round (sd3 * 10000) /10000.0); This keeps 4 bits
//// Method 2
DecimalFormat df2 = new DecimalFormat ("###. 00");
DecimalFormat df3 = new DecimalFormat ("###. 000");
System.out.println ("Conversion 2 =" + df2.format (1 / (float) 8));
System.out.println ("Conversion 3 =" + df3.format (1.1256));
//// Method 3
// String ss = String.format ("% 10.2f %%", 1.1256); //1.13f
// String ss = String.format ("% 10.2f", 1.1256); // 1.13
String ss = String.format ("% 1.2f", 1.1256); //1.13
System.out.println ("Conversion 4 =" + ss);
//// Method 4
double x = 23.5455;
NumberFormat ddf1 = NumberFormat.getNumberInstance ();
ddf1.setMaximumFractionDigits (2);
String s = ddf1.format (x);
System.out.print ("Number format =" + s); // rounded 23.55
//// Method 5
float T = 0.1257f;
BigDecimal b = new BigDecimal (T);
T = b.setScale (2, BigDecimal.ROUND_HALF_UP) .floatValue ();
System.out.println ("Retain 2 decimal places:" + T);
JAVA float Double data type reserved 2-bit decimal point 5 ways