{"id":1846,"date":"2018-01-05T08:22:27","date_gmt":"2018-01-05T08:22:27","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=1846"},"modified":"2024-09-04T06:53:57","modified_gmt":"2024-09-04T06:53:57","slug":"how-to-round-a-double-value-in-java","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-round-a-double-value-in-java\/","title":{"rendered":"How to round a double value in Java."},"content":{"rendered":"<h2>How to round a double value in Java.<\/h2>\n<p>In this post, we will learn how to round a double value in Java with the help of a small program.<br \/>\nFirst, let&#8217;s perform some calculation and try to print the double variable as shown below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * \r\n *\/\r\npackage questions;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class RoundDoubleExample {\r\n  public static void main(String[] args) {\r\n    double l;  \/\/ liters\r\n    double g; \/\/ gallons \r\n    Scanner keyboard = new Scanner( System.in );\r\n    System.out.print( \"Enter liters := \" ); \r\n    l = keyboard.nextDouble( ); \r\n    g = 0.264 * l;  \r\n    System.out.print( l + \" Liters = \" );\r\n    System.out.println( g + \" gallons.\" );\r\n    keyboard.close();\r\n  } \r\n} \r\n<\/pre>\n<p>Sample output of the program:<\/p>\n<p>Enter liters := 3.785<br \/>\n3.785 Liters = 0.9992400000000001 gallons.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1847\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value.png\" alt=\"\" width=\"1761\" height=\"1003\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value.png 1761w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value-300x171.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value-1024x583.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value-768x437.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Not-Round-Double-Value-1536x875.png 1536w\" sizes=\"auto, (max-width: 1761px) 100vw, 1761px\" \/><\/p>\n<p>There are so many decimals printed in the output. In some cases, we may want to round the values to 2 or three decimal places. One reason could be the consistency in the output format. Lets us round the value to 3 decimal places.<\/p>\n<h3>Math.round()<\/h3>\n<p>We can round the variable to 3 decimal places by using Math.round() function.<\/p>\n<p>(double)Math.round(gallons * 1000d) \/ 1000d<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * \r\n *\/\r\npackage questions;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class RoundDoubleExample {\r\n  public static void main(String[] args) {\r\n    double l;  \/\/ liters\r\n    double g; \/\/ gallons \r\n    Scanner keyboard = new Scanner( System.in );\r\n    System.out.print( \"Enter liters := \" ); \r\n    l = keyboard.nextDouble( ); \r\n    g = 0.264 * l;  \r\n    System.out.print( l + \" Liters = \" );\r\n    System.out.println( (double)Math.round(g * 1000d) \/ 1000d + \" gallons.\" );\r\n    keyboard.close();\r\n  } \r\n} \r\n<\/pre>\n<p>Enter liters := 3.785<br \/>\n3.785 Liters = 0.999 gallons.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1848\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round.png\" alt=\"\" width=\"1741\" height=\"993\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round.png 1741w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round-300x171.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round-1024x584.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round-768x438.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Rounding-USing-Math-Round-1536x876.png 1536w\" sizes=\"auto, (max-width: 1741px) 100vw, 1741px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Using DecimalFormat<\/h3>\n<p>DecimalFormat df = new DecimalFormat(&#8220;0.000&#8221;);<\/p>\n<p>use df.format() to display the variable to round to 3 decimal places.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">\/**\r\n * \r\n *\/\r\npackage questions;\r\n\r\nimport java.text.DecimalFormat;\r\nimport java.util.Scanner;\r\n\r\npublic class RoundDoubleExample {\r\n  \r\n  private static DecimalFormat df = new DecimalFormat(\"0.000\");\r\n  public static void main(String[] args) {\r\n    double l;  \r\n    double g;  \r\n    Scanner keyboard = new Scanner( System.in );\r\n    System.out.print( \"Enter liters := \" ); \r\n    l = keyboard.nextDouble( ); \r\n    g = 0.264 * l;  \r\n    System.out.print( l + \" Liters = \" );\r\n    System.out.println( df.format(g) + \" gallons.\" );\r\n    keyboard.close();\r\n  } \r\n} \r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1849\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format.png\" alt=\"\" width=\"1740\" height=\"989\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format.png 1740w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format-300x171.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format-1024x582.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format-768x437.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Using-Decimal-format-1536x873.png 1536w\" sizes=\"auto, (max-width: 1740px) 100vw, 1740px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to round a double value in Java. In this post, we will learn how to round a double value in Java with the help of a small program. First, let&#8217;s perform some calculation and try to print the double variable as shown below: \/** * *\/ package questions; import java.util.Scanner; public class RoundDoubleExample { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1846","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1846"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1846\/revisions"}],"predecessor-version":[{"id":23998,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1846\/revisions\/23998"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}