Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
System.out.printf¶
System.out.printf("Pi: %.2f", 3.1415926)Pi: 3.14nullString.format¶
String fval = String.format("%1.2f", 3.1415926);
System.out.print(fval);3.14nulljava.text.DecimalFormat¶
import java.text.DecimalFormat;
DecimalFormat dfmat = new DecimalFormat("#.##");
double fval = Double.valueOf(dfmat.format(3.1415926));
System.out.print(fval);3.14nullMath.round¶
import java.lang.Math;
double fval = Math.round(3.1415926);
System.out.print(fval);3.0nullimport java.lang.Math;
double fval = Math.round(3.1415926 * 100) / 100.0;
System.out.print(fval);3.14nullimport org.apache.commons.math3.util.Precision;
double fval = Precision.round(3.1415926, 2);
System.out.print(fval);