How to combine two or two decimal places in Java?

This is what I did to round up to two decimal places:

amount = roundTwoDecimals(amount); public double roundTwoDecimals(double d) { DecimalFormat twoDForm = new DecimalFormat("#.##"); return Double.valueOf(twoDForm.format(d)); } 

This works great if sum = 25.3569 or something like that, but if sum = 25.00 or sum = 25.0, then I get 25.0! I want to round and format to two decimal places.

+62
java double
Apr 19 2018-11-11T00:
source share
21 answers

Do you work with money? Creating a String and then converting it is pretty closed.

Use BigDecimal . This has been discussed quite widely. You must have the Money class, and the amount must be BigDecimal .

Even if you are not working with money, consider BigDecimal .

+19
Apr 19 '11 at 2:23
source share

Just use: (just like a pie)

 double number = 651.5176515121351; number = Math.round(number * 100); number = number/100; 

The output will be 651.52

+51
Jun 12 '13 at 18:42
source share

Use the digit holder ( 0 ) since the " # " trailing / leading zeros show as missing:

 DecimalFormat twoDForm = new DecimalFormat("#.00"); 
+19
Apr 19 2018-11-11T00:
source share

You cannot "double a number to [number] decimal places" because doubles do not have decimal places. You can convert double to base-10 String with N decimal places, because base-10 has decimal places, but when you translate it back, you return to double ground with binary fractional places.

+10
Apr 19 '11 at 1:01
source share

Use this

String.format ("%. 2f", doubleValue) // change 2 as per your requirement.

+6
Aug 03 '15 at 11:52
source share

This is the easiest way I could do this, but it makes the job a lot easier than most of the examples reviewed.

  double total = 1.4563; total = Math.round(total * 100); System.out.println(total / 100); 

The result is 1.46.

+5
Jan 20 '14 at 3:01
source share

You can use org.apache.commons.math.util.MathUtils from apache common

double round = MathUtils.round(double1, 2, BigDecimal.ROUND_HALF_DOWN);

+4
Jun 13 '13 at 15:45
source share
+2
Mar 24 '16 at 23:08
source share

Your Money class can be represented as a subclass of Long or having a member that represents monetary value as a native. Then, when you assign values ​​to your instances of money, you will always store values ​​that are actually real monetary values. You simply output your Money object (via the Money overridden toString () method) with the appropriate formatting. for example, $ 1.25 in the Money Object internal view is 125. You represent money in cents or pence, or as the minimum denomination in the currency you seal ... then format it at the exit. No, you can never save an β€œillegal” cash value, for example, $ 1.257.

+1
Dec 05 '11 at 11:58
source share

double amount = 25.00;

NumberFormat formatter = new DecimalFormat ("# 0.00");

System.out.println (formatter.format (amount));

+1
Nov 04 '14 at 19:41
source share

You can try the following:

 public static String getRoundedValue(Double value, String format) { DecimalFormat df; if(format == null) df = new DecimalFormat("#.00"); else df = new DecimalFormat(format); return df.format(value); } 

or

 public static double roundDoubleValue(double value, int places) { if (places < 0) throw new IllegalArgumentException(); long factor = (long) Math.pow(10, places); value = value * factor; long tmp = Math.round(value); return (double) tmp / factor; } 
+1
Feb 20 '15 at 10:39
source share
 DecimalFormat df = new DecimalFormat("###.##"); double total = Double.valueOf(val); 
+1
Nov 11 '16 at 10:13
source share

If you want the result to be two decimal places, you can do

 // assuming you want to round to Infinity. double tip = (long) (amount * percent + 0.5) / 100.0; 

This result is not accurate, but Double.toString (double) will correct this and print one to two decimal places. However, as soon as you perform another calculation, you can get a result that will not be implicitly rounded .;)

0
Apr 19 '11 at 9:01
source share

Math.round - one answer,

 public class Util { public static Double formatDouble(Double valueToFormat) { long rounded = Math.round(valueToFormat*100); return rounded/100.0; } } 

Test at Spock, Groovy

 void "test double format"(){ given: Double performance = 0.6666666666666666 when: Double formattedPerformance = Util.formatDouble(performance) println "######################## formatted ######################### => ${formattedPerformance}" then: 0.67 == formattedPerformance } 
0
Jul 14 '14 at 12:23
source share

Assuming that the sum can be either positive or negative, rounding to two decimal places can use the following code fragment.

 amount = roundTwoDecimals(amount); public double roundTwoDecimals(double d) { if (d < 0) d -= 0.005; else if (d > 0) d += 0.005; return (double)((long)(d * 100.0))/100); } 
0
Jul 14 '14 at 12:37
source share

By running java 1.8, you can do more with lambda expressions and check for null. In addition, the one below can handle Float or Double and a variable number of decimal points (including 2: -)).

 public static Double round(Number src, int decimalPlaces) { return Optional.ofNullable(src) .map(Number::doubleValue) .map(BigDecimal::new) .map(dbl -> dbl.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP)) .map(BigDecimal::doubleValue) .orElse(null); } 
0
Mar 17 '16 at 18:33
source share

where num is a double number

  • The integer 2 indicates the number of decimal places that we want to print.
  • Here we take 2 decimal pallets

    System.out.printf ("% 2f"., Qty);

0
Sep 19 '16 at 16:46
source share

Here is a simple way to guarantee the output of myFixedNumber, rounded to two decimal places:

 import java.text.DecimalFormat; public class TwoDecimalPlaces { static double myFixedNumber = 98765.4321; public static void main(String[] args) { System.out.println(new DecimalFormat("0.00").format(myFixedNumber)); } } 

Result: 98765.43

0
Dec 05 '16 at 5:07
source share

First declare an object of class DecimalFormat . Note that the argument inside DecimalFormat is #.00 , which means exactly 2 decimal rounding points.

 private static DecimalFormat df2 = new DecimalFormat("#.00"); 

Now apply the format to your double value:

 double input = 32.123456; System.out.println("double : " + df2.format(input)); // Output: 32.12 

Note in the case of double input = 32.1;

Then the output will be 32.10 , etc.

0
Jun 05 '17 at 6:35
source share
  int i = 180; int j = 1; double div= ((double)(j*100)/i); DecimalFormat df = new DecimalFormat("#.00"); // simple way to format till any deciaml points System.out.println(div); System.out.println(df.format(div)); 
0
Oct 18 '17 at 4:19 on
source share

You can use this function.

 import org.apache.commons.lang.StringUtils; public static double roundToDecimals(double number, int c) { String rightPad = StringUtils.rightPad("1", c+1, "0"); int decimalPoint = Integer.parseInt(rightPad); number = Math.round(number * decimalPoint); return number/decimalPoint; } 
0
Aug 08 '19 at 5:36
source share



All Articles