rounding off float to 2 decimal places in java code example
Example 1: java round double to 2 decimal places
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class DecimalExample {
private static DecimalFormat df = new DecimalFormat("0.00");
public static void main(String[] args) {
double input = 1205.6358;
System.out.println("salary : " + input);
System.out.println("salary : " + df.format(input));
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("salary : " + df.format(input));
df.setRoundingMode(RoundingMode.UP);
System.out.println("salary : " + df.format(input));
}
}
Example 2: round off java 2 decimal places
class round{
public static void main(String args[]){
double a = 123.13698;
double roundOff = Math.round(a*100)/100;
System.out.println(roundOff);
}
}