How to Round Number upto n decimal places in java.
import java.util.*;
import java.text.DecimalFormat;
class MyDecimalFormater
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
try
{
double price = 1234567890.9876;
// round up to 2 decimal places
// fist method
System.out.println("Using Math.round() : "+Math.round(price*100.0)/100.0);
// second method
System.out.println("By Using DecimalFormat :");
DecimalFormat f = new DecimalFormat("###.00");
System.out.println("round upto 2 places : "+f.format(price));
// exploring DecimalFormat
DecimalFormat g = new DecimalFormat("$###.000");
System.out.println("round upto 3 places and using dollar symbol : "+g.format(price));
DecimalFormat h = new DecimalFormat("\u00A5###,###.000");
System.out.println("adding unicode and , : "+h.format(price));
}
catch(Exception e){
return;
}
}
}