The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java example program to round double to 2 decimal places

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java example program to round double to 2 decimal places Posted: Apr 23, 2017 6:46 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java example program to round double to 2 decimal places
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • To round the decimal number in java we have DecimalFormat class in java.
  • By using DecimalFormat class format() method we can round double or float number to N decimal places.
  • Lets see a java program on how to round double to 2 decimal places.



 Java Program to round double number to 2 / 3 decimal places.


  1. package com.javarounddecimal;
  2. import java.text.DecimalFormat;
  3.  
  4. public class RoundDecimal {
  5.     /**
  6.      * java round double to 2 decimal places
  7.      * @author www.instanceofjava.com
  8.      */
  9.     public static void main(String[] args) {
  10.         
  11.         double number = 12.3712377;
  12.         DecimalFormat df1 = new DecimalFormat("#.##");
  13.         System.out.println(number + " is rounded to: " + df1.format(number));
  14.          
  15.         DecimalFormat df2 = new DecimalFormat("#.###");
  16.         System.out.println(number + " is rounded to: " + df2.format(number));
  17.          
  18.         number = 12.388654;
  19.         
  20.         DecimalFormat df3 = new DecimalFormat("#.##");
  21.         System.out.println(number + " is rounded to: " + df3.format(number));
  22.        
  23.         
  24.         DecimalFormat df4 = new DecimalFormat("#.###");
  25.         System.out.println(number + " is rounded to: " + df4.format(number));
  26.  
  27.     }
  28.  
  29. }
 Output:


  1. 12.3712377 is rounded to: 12.37
  2. 12.3712377 is rounded to: 12.371
  3. 12.388654 is rounded to: 12.39
  4. 12.388654 is rounded to: 12.389

  Java Program to round float number to 2 / 3 decimal places.


java round float to 2 decimal places




Output:

  1. 12.371238 is rounded to: 12.37
  2. 12.371238 is rounded to: 12.371
  3. 12.388654 is rounded to: 12.39
  4. 12.388654 is rounded to: 12.389

Read: Java example program to round double to 2 decimal places

Topic: The news from Docker-land, plus, the money being fought over - Notebook Previous Topic   Next Topic Topic: Java Collections interview questions and answers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use