7

I am trying to keep two decimal places, even if then numbers are zeroes, using DecimalFormatter:

DecimalFormat df = new DecimalFormat("#.00");

m_interest   = Double.valueOf(df.format(m_principal * m_interestRate));
m_newBalance = Double.valueOf(df.format(m_principal + m_interest - m_payment));
m_principal  = Double.valueOf(df.format(m_newBalance));

However for some values this gives two decimal places, and for others it doesnt. How can i fix this?

Lion
  • 18,729
  • 22
  • 80
  • 110
user3044874
  • 177
  • 1
  • 3
  • 14
  • Can you give an example which didn't work? – Rahul Dec 09 '13 at 04:34
  • 1
    Are you sure? I would use "#0.00" for my format String, but this should and will work. I have to wonder if you are in fact running the code that you show above. – Hovercraft Full Of Eels Dec 09 '13 at 04:34
  • Possibly because you are doing `Double.valueOf`. Just a guess, though. – Michael Yaworski Dec 09 '13 at 04:38
  • @R.J if i set m_interest to be 10000.00, it prints 10000.0. – user3044874 Dec 09 '13 at 04:39
  • 1
    Are you formatting `m_interest` using the `DecimalFormat` before printing it? Because I can see you doing `Double.valueOf()` on the final result and if you're going to print that as such, then you're bound to get varying decimal points. – Rahul Dec 09 '13 at 04:41
  • 1
    Follow [Java naming convention](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). – Lion Dec 09 '13 at 04:45

5 Answers5

8

It is because you are using Double.valueOf on the DecimalFormat and it is converting the formatted number back to a double, therefore eliminating the trailing 0s.

To fix this, only use the DecimalFormat when you are displaying the value.

If you need m_interest calculations, keep it as a regular double.

Then when displaying, use:

System.out.print(df.format(m_interest));

Example:

DecimalFormat df = new DecimalFormat("#.00");
double m_interest = 1000;
System.out.print(df.format(m_interest)); // prints 1000.00
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • But I need to send value as double. Here df.format() output is of string type. Can somehow i send double value as 10.00, instead 10.0 ? Or I can't at all ? – ChandraBhan Singh Dec 14 '17 at 12:42
  • @ChandraBhanSingh That doesn't really make sense. A double is a numerical value; it is not formatted to have trailing zeros. If you need to print the number in some way, use `df.format()` before doing so. Besides printing the number, you can just leave it as a decimal. What situation is concerning you? – Michael Yaworski Dec 14 '17 at 18:06
  • In my project, we have signed off a document explicitly mentioning that double value would be off 4 precision. Now client is like we want those 4 zero after decimal points, i am really confused how to achieve this. Appreciate if you can tell me some catchy statements, so i can convince him regarding this. I am sending those values over JAX-WS webservice. – ChandraBhan Singh Dec 15 '17 at 04:01
5

DecimalFormat and NumberFormat should work just fine. A currency instance could work even better:

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Foo {
   public static void main(String[] args) {
      DecimalFormat df = new DecimalFormat("#0.00");

      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumFractionDigits(2);
      nf.setMaximumFractionDigits(2);

      NumberFormat cf = NumberFormat.getCurrencyInstance();

      System.out.printf("0 with df is: %s%n", df.format(0));
      System.out.printf("0 with nf is: %s%n", nf.format(0));
      System.out.printf("0 with cf is: %s%n", cf.format(0));
      System.out.println();
      System.out.printf("12345678.3843 with df is: %s%n",
            df.format(12345678.3843));
      System.out.printf("12345678.3843 with nf is: %s%n",
            nf.format(12345678.3843));
      System.out.printf("12345678.3843 with cf is: %s%n",
            cf.format(12345678.3843));
   }
}

This would output:

0 with df is: 0.00
0 with nf is: 0.00
0 with cf is: $0.00

12345678.3843 with df is: 12345678.38
12345678.3843 with nf is: 12,345,678.38
12345678.3843 with cf is: $12,345,678.38
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

Use BigDecimal instead, which supports the formatting approach you seek.

This question details it: How to print formatted BigDecimal values?

Community
  • 1
  • 1
Rob
  • 11,446
  • 7
  • 39
  • 57
  • 1
    Oh it has overhead? What would that be? If you are doing money, BigDecimal is how you do it. – Rob Dec 09 '13 at 04:36
  • 1
    Sorry, you are correct. All financial calculations should be done with BigDecimal. I retract my prior comment. Again, sorry. 1+ – Hovercraft Full Of Eels Dec 09 '13 at 04:39
  • 1
    as @Rob points out, use BigDecimal, not so much because of the formatting available, but because it uses infinite precision, while Double will be subject to rounding that will be undesirable for monetary calculations. – GreyBeardedGeek Dec 09 '13 at 04:40
0
m_interest = Double.valueOf(String.format("%.2f", sum));
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • 2
    While this code may answer the question, it would be better to explain _how_ it solves the problem without introducing others and _why_ to use it. Code-only answers are not useful in the long run. – Benjamin W. Jan 29 '16 at 23:52
-1

Why don't you simply Math.round(value * 100) / 100.0 ?

pinxue
  • 1,736
  • 12
  • 17
  • This is exactly what his problem is. He needs it to show trailing 0s. Rounding will eliminate them. This is also not an answer, it's a question, therefore should be a comment. – Michael Yaworski Dec 09 '13 at 04:46
  • Sorry, I don't think Double.valueOf() cares about those 0s, thus I guess it is overkilling to take the trouble of formatting. – pinxue Dec 09 '13 at 04:51
  • Yes, the `Double.valueOf()` is his problem. That's what we're trying to fix for him. Please read his question. – Michael Yaworski Dec 09 '13 at 04:52