2

I have been wondering around and found a lot of ways to remove .0 from doubles when formatting to strings but never found how to keep stuff for prices.

DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
switch (position){
    case 0:price = 11.90;break;
    case 1:price = 1.2;break;
    case 2:price = 1.002;break;
    case 3:price = 1.0000;break;
}
System.out.println(format.format(price));
// 11.9
// or 1.2 
// or 1.002
// or 1

but what i need is womthing like this

// 11.90
// 1.2 
// or 1.002
// or 1 

This is not a duplicate because that one will add 00 to any price which I don't want ... I am only looking to remove the floating point if there are zeros after it but to keep any zero there if there is a number.

e.g.

1.0, 1.00 or 1.00000
should print out 1

but

1.90, 1.900 or 1.90000
should print out the same 1.90, 1900 or 1.900000
weston
  • 54,145
  • 21
  • 145
  • 203
Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
  • You really [shouldn't be using `double` for currency](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) in the first place. – azurefrog Mar 21 '17 at 15:33
  • You may have to specify the format at the same time you specify the price, both in the switch ... case. Because there isn't really a pattern in the printing formats you're specifying. Or print them as strings, and not doubles. – AntonH Mar 21 '17 at 15:35
  • @AntonH the pattern i am looking for is to remove the floating decimal "." only when its not there so any number with x.0, x.00 or x.0000000 will be x but x.20 will be x.20 – Yamen Nassif Mar 21 '17 at 15:37
  • @azurefrog i understand but this is something i have to live with ... – Yamen Nassif Mar 21 '17 at 15:37
  • @YamenNassif But there isn't a representational difference between 11.90 and 1.2 (you could say 11.90 and 1.20, or 11.9 and 1.2). In what cases are you displaying one, two, or more decimal points? And if you *know*, why wouldn't you specify the format at that time (i.e., in the `switch...case`, where you create a new format that is used when you print out the value). – AntonH Mar 21 '17 at 15:43
  • well i understand the maths but Germans does not hehe... – Yamen Nassif Mar 21 '17 at 15:44
  • the switch case is just a representation of the data i have.. the data is being received from a JSON. – Yamen Nassif Mar 21 '17 at 15:47
  • 1
    When should the trailing 0s be removed? Why should it be removed for 1.2 but not 1.90? – Vince Mar 21 '17 at 15:49
  • @VinceEmigh it only should be remove when its directly after the floating point without any trailing numbers ie. 1.0, 1.00 but not 1.01 or 1.001 or 1.10 – Yamen Nassif Mar 21 '17 at 15:56

2 Answers2

2

1.90, 1.900 or 1.90000 should print out the same 1.90, 1900 or 1.900000

You can't keep trailing zeros on a primitive, so 0.9 is the exact same constant as 0.90.

C# has decimal but Java has no primitive datatype for this purpose.

Java does have a class equivalent with similar behaviour, BigDecimal:

BigDecimal d1 = BigDecimal.valueOf(9, 1);  //  9/10^1
BigDecimal d2 = BigDecimal.valueOf(90, 2); //  9/10^2
System.out.println(d1); //0.9
System.out.println(d2); //0.90

http://ideone.com/1gIgIR

1.0, 1.00 or 1.00000 should print out 1

Once using BigDecimal, it's not so hard to check if it is a whole number:

public static boolean isIntegerValue(BigDecimal bd) {
  return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0;
}

https://stackoverflow.com/a/12748321/360211

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
0

It's difficult to understand the exact intention you have here. I can understand that you might want to represent 1.000 as 1 (there is an actual difference between the two values since one is a float or double and the other is an int or long, but as there is no difference between 11.9 and 11.90 (they are represented with the same object), it might be very difficult to differenticate the two. My advice, beyond writing down exactly the conditions for each format, would be to get the original value of price in a String and then apply the format as a function of the string (it's then easy to match endsWith(".000") or whatever and retain the original format in the other cases.

Olivier Durin
  • 13
  • 1
  • 3