2

I have tried the following code but it is not working in a particular case.

Eg: Suppose, I have a double value=2.5045 and i want it to be rounded off upto two decimal places using the below code.After rounding off, i get the answer as 2.5. But I want the answer to be 2.50 instead. In this case,zero is trimmed off. Is there any way to retain the zero so as to get the desired answer as 2.50 after rounding off.

 private static DecimalFormat twoDForm = new DecimalFormat("#.##");
         public static double roundTwoDecimals(double amount) {
                return Double.valueOf(twoDForm.format(amount));
            }
zam
  • 29
  • 1
  • 6
  • `double` doesn't have a format it is just a value. What you want is a `String` and DecimalFormat is one way to get it. – Peter Lawrey Feb 12 '14 at 12:18

8 Answers8

3

try this pattern

new DecimalFormat("0.00");

but this will change only formatting, double cannot hold number of digits after decimal poin, try BigDecimal

BigDecimal bd = new BigDecimal(2.5045).setScale(2, RoundingMode.HALF_UP);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • I tried this approach, but it still returns me an answer of 2.5. – zam Feb 12 '14 at 12:23
  • try System.out.println(new DecimalFormat("0.00").format(2.5045)); – Evgeniy Dorofeev Feb 12 '14 at 12:31
  • The above code is working but i want it to convert it back to Double like the way I am doing. So while converting back to Double,the zero is trimmed off. So is there any other way around? – zam Feb 12 '14 at 13:12
  • Now I understood. You cant have that with double. You can do what you want with BigDecimal which has precision prop. – Evgeniy Dorofeev Feb 12 '14 at 13:29
  • Ya you are right. I am not able to do that with Double.Can you provide me with an example with BigDecimal if possible? – zam Feb 12 '14 at 14:11
  • see update, you will decide if you can use it, but this thing remembers the number of digits after decimal point – Evgeniy Dorofeev Feb 12 '14 at 14:24
  • Thanks for the code.It works fine.I guess this is not possible with double. – zam Feb 12 '14 at 14:29
1

Look at the documentation for DecimalFormat. For # it says:

Digit, zero shows as absent

0 is probably what you want:

Digit

So what you are looking for is either "0.00" or "#.00" as a format string, depending on whether you want the first digit before the period, to be visible in case the numbers absolute value is smalle than 0.

1

Try this

DecimalFormat format = new DecimalFormat("#");  
format.setMinimumFractionDigits(2);  
answer.setText(format.format(data2));
Programmer
  • 455
  • 5
  • 18
  • This is working fine but the answer is in string. I want it to be converted to Double using valueOf method like the way i am doing in my code. After conversion,the trailing zero is trimmed off.So is there any way around? – zam Feb 12 '14 at 13:15
  • @zam You can use `Double.parseDouble()` to convert a `String` to a `double`: `String text = "12.34"; // example String` `double value = Double.parseDouble(text);` For your case it looks like you want: `double total = Double.parseDouble(answer.setText(format.format(data2)));` – Programmer Feb 12 '14 at 13:41
  • Thanks for the code. I just tried it. It is still giving the same.It goes well with the String. But creates a problem while doing with double value. – zam Feb 12 '14 at 14:17
  • @zam `double d = Double.parseDouble(aString);` this would convert string to double.. – Programmer Feb 12 '14 at 14:19
  • @zam This works `new DecimalFormat("#,###,###,##0.00##").format(d)` – Programmer Feb 12 '14 at 14:35
  • I tried the above one. It is giving me upto 4 decimal points.Rounding off is not done upto 2 decimal place. – zam Feb 12 '14 at 14:55
  • @zam `DecimalFormat df = new DecimalFormat("###,##0.00");` This works – Programmer Feb 12 '14 at 14:57
0

Try This

double d = 4.85999999999;   
long l = (int)Math.round(d * 100); // truncates   
d = l / 100.0;  
Programmer
  • 455
  • 5
  • 18
  • if there is a 0 at the secound decimal place and after the null a values less than 5 it prints x.x thats the problem of the op. – kai Feb 12 '14 at 12:07
  • refer this http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html – Programmer Feb 12 '14 at 12:24
  • input: d = 4.800999; ouput: 4.8 and thats not the ouput he want...i tested it. it should be 4.80 – kai Feb 12 '14 at 12:29
  • its 480 and after dividing it by 100.0 its 4.8 – kai Feb 12 '14 at 12:32
  • got it... check the new answer that i have posted – Programmer Feb 12 '14 at 12:35
  • i just checked the above piece of code. It is showing 4.8. – zam Feb 12 '14 at 13:07
  • @user3301373 ya so please try the new code that i have posted that works as u expect.. – Programmer Feb 12 '14 at 13:09
  • think you can delete this. – kai Feb 12 '14 at 13:18
  • i tried the above code.but stil the same.Have you edited your code already?I tried this::: double d = 4.85999999999; long l = (int)Math.round(d * 100); // truncates d = l / 100.0; – zam Feb 12 '14 at 14:09
  • @zam i have posted two answers one is where you get the answer in form of string(if u remember u had commented) and this. you can try what i have mentioned there .. that works fine – Programmer Feb 12 '14 at 14:16
  • but the above code is giving me as 4.8.The trailing zero is trimmed off.Can you please add your code in the comment section if you dont mind? – zam Feb 12 '14 at 14:25
  • DecimalFormat format = new DecimalFormat("#"); format.setMinimumFractionDigits(2); answer.setText(format.format(yourDoubleValue)); – kai Feb 12 '14 at 15:21
0

You are returning a double. But double or Double are objects representing a number and don't carry any formatting information. Ìf you need to output two decimal places the point to do this is when you convert your double to a String.

piet.t
  • 11,718
  • 21
  • 43
  • 52
0

use # if you want to ignore 0

new DecimalFormat("###,#0.00").format(d)
Programmer
  • 455
  • 5
  • 18
0

There is another way to achieve this . I have already posted answer in post will just answer again here. As we will require rounding off values many times .

public class RoundingNumbers {
    public static void main(String args[]){
        double number = 2.5045;
        int decimalsToConsider = 2;
        BigDecimal bigDecimal = new BigDecimal(number);
        BigDecimal roundedWithScale = bigDecimal.setScale(decimalsToConsider, BigDecimal.ROUND_HALF_UP);
        System.out.println("Rounded value with setting scale = "+roundedWithScale);

        bigDecimal = new BigDecimal(number);
        BigDecimal roundedValueWithDivideLogic = bigDecimal.divide(BigDecimal.ONE,decimalsToConsider,BigDecimal.ROUND_HALF_UP);
        System.out.println("Rounded value with Dividing by one = "+roundedValueWithDivideLogic);

    }
}

Output we will get is

Rounded value with setting scale = 2.50
Rounded value with Dividing by one = 2.50
Community
  • 1
  • 1
Shirishkumar Bari
  • 2,692
  • 1
  • 28
  • 36
0
double kilobytes = 1205.6358;
double newKB = Math.round(kilobytes*100.0)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
System.out.println("kilobytes (DecimalFormat) : " + df.format(kilobytes));

Try this if u are still getting the above problem

Tanvi Jain
  • 113
  • 1
  • 14