System.out.printf¶
In [3]:
System.out.printf("Pi: %.2f", 3.1415926)
Out[3]:
String.format¶
In [15]:
String fval = String.format("%1.2f", 3.1415926);
System.out.print(fval);
Out[15]:
java.text.DecimalFormat¶
In [9]:
import java.text.DecimalFormat;
DecimalFormat dfmat = new DecimalFormat("#.##");
double fval = Double.valueOf(dfmat.format(3.1415926));
System.out.print(fval);
Out[9]:
Math.round¶
In [12]:
import java.lang.Math;
double fval = Math.round(3.1415926);
System.out.print(fval);
Out[12]:
In [14]:
import java.lang.Math;
double fval = Math.round(3.1415926 * 100) / 100.0;
System.out.print(fval);
Out[14]:
In [ ]:
import org.apache.commons.math3.util.Precision;
double fval = Precision.round(3.1415926, 2);
System.out.print(fval);