How to round a Double value in Java

With this code, you can round a double in Java

  1. Using DecimalFormat method:
    double d = 1.234567;
    DecimalFormat df = new DecimalFormat("#.##");
    System.out.print(df.format(d));
  2. Using BigDecimal method:
    public static void main(String[] args)
    {
          double value = 1254.625;
          String val = value+"";
          BigDecimal big = new BigDecimal(val);
          big = big.setScale(2, RoundingMode.HALF_UP);
          System.out.println("Number : "+big);
    }

Reference link: http://javafox.wordpress.com/2009/10/20/redondear-un-double-en-java/

One thought on “How to round a Double value in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s