33

I got this problem with double (decimals).
When a double = 1.234567 Then I use String.format("%.3f", myString);
So the result is 1.234

But when my double is 10
The result will be 10,000
I want this to be 10

Is their a way to say that he only needs to show the decimals when it is "usefull"?

I saw some posts about this, but that was php or c#, couldn't find something for android/java about this (maybe I don't look good).

Hope you guys can help me out with this.

Edit, for now I use something like this: myString.replace(",000", "");
But I think their is a more "friendly" code for this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bigflow
  • 3,616
  • 5
  • 29
  • 52

3 Answers3

95

The DecimalFormat with the # parameter is the way to go:

public static void main(String[] args) {

        double d1 = 1.234567;
        double d2 = 2;
        NumberFormat nf = new DecimalFormat("##.###");
        System.out.println(nf.format(d1));
        System.out.println(nf.format(d2));
    }

Will result in

1.235
2
jolivier
  • 7,380
  • 3
  • 29
  • 47
  • This code doesn't take into account the user's locale. For example with the Dutch locale, the decimal number should be written with a coma: "1,235" instead of "1.235". – makovkastar Aug 22 '17 at 11:53
  • 5
    Yes it does. The "." I give as parameter to the DecimalFormat does not mean "write a dot", it means "write the decimal separator or monetary decimal separator " see https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html . I just tested to be sure by setting the default locale to french at the beginning of the function and the dots were effectively replaced by comas in the output, as expected. – jolivier Aug 22 '17 at 12:14
  • 2
    You are right, sorry, my mistake. This code properly formats the decimal separator according to the user's locale. This code is also working for me: NumberFormat.getNumberInstance().format(power)). It just doesn't allow to set the precision. – makovkastar Aug 22 '17 at 12:18
0

Don't use doubles. You can lose some precision. Here's a general purpose function.

public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}

You can call it with

round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);

"precision" being the number of decimal points you desire.

Copy from Here.

Community
  • 1
  • 1
Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Hey, thanks for the reply. I tried it, I now get (example) from 10,000 it goes to 10.0 , so still one decimal, any idea how to "fix" this? Or what I am doing wrong. – Bigflow Aug 06 '12 at 10:37
  • the same, but found something interesting: when I got this number 123,405 and set "precision" to 2, it will make 123,4 (what is good) but if you got a "round" number, it always set ,0 behind it. So 10 -> 10,0. – Bigflow Aug 06 '12 at 10:45
  • jolivier's answer worked for me, but thanks for helping me! (an up-vote worth) – Bigflow Aug 06 '12 at 10:54
  • 1
    This feels a bit over the top. There's nothing special about base10 over base2 or base3. A blanket "never use doubles" feels like a recipe for unnecessarily slow and awkward programs. If you're measuring money then a double is obviously inappropriate, if you're measuring the length of a wall its fine (As far as I'm aware BigDecimal would have trouble with a wall that was "one third of a meter" long in just the same way as the double would) – Richard Tingle Nov 10 '17 at 11:06
0

Try it

double amount = 1.234567 ;
  NumberFormat formatter = new DecimalFormat("##.###");
  System.out.println("The Decimal Value is:"+formatter.format(amount));
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37