-2

I have some code that checks how many decimals my number has.

Example:

1,10 correct conversion: String.format("%.1f", Double.parseDouble(md.get("amount"))

1,10 incorrect conversion: String.format("%.2f", Double.parseDouble(md.get("amount"))

I cannot have trailing 0's

1 correct conversion: String.format("%.0f", Double.parseDouble(md.get("amount"))

1 incorrect conversion: String.format("%.1f", Double.parseDouble(md.get("amount"))

I cannot have trailing 0's

For numbers larger than 1 000 000 Java converts my numbers to scientific notation, and the only way i know to convert them back without losing decimals is to use String.format.

My bad code:

md is a map, and the key "amount" maps to a String that is supposed to be a Double

if( Double.parseDouble(md.get("amount")) * 10 % 10 != 0) {
  System.out.println("0,1");
  md.put("amount", String.format("%.1f", Double.parseDouble(md.get("amount"))));
  if( Double.parseDouble(md.get("amount")) * 100 % 10 != 0) {
    System.out.println("0,11");
    md.put("amount", String.format("%.2f", Double.parseDouble(md.get("amount"))));
  }
}
else if(Double.parseDouble(md.get("amount")) * 10 % 10 == 0 && Double.parseDouble(md.get("amount")) * 100 % 10 == 0){
  System.out.println("0,0");
  md.put("amount", String.format("%.0f", Double.parseDouble(md.get("amount"))));
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Link
  • 361
  • 5
  • 17
  • Yeah, the question is if there is another way of doing this, that isn't this messy – Link Jan 07 '20 at 12:37
  • 1
    Do you know the class `BigDecimal`? – deHaar Jan 07 '20 at 12:41
  • If it is a String to begin with, can't you just strip off the trailing zeros after the decimal point and handle the special case of where there is no remaining decimal (remove the decimal point itself). – DanielBarbarian Jan 07 '20 at 12:44

3 Answers3

0
String s = md.get("amount").replaceFirst("(\\.(\\d*[1-9])?)0+$", "$1");

A double is just an approximation, 0.1 will not be exact: 0.09999987 or such.

One normally would use a BigDecimal with toPlainString for non-scientific notation.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

To find how many decimals you have , you can use scale() from BigDecimal. Because your number is a string, you must convert to BigDecimal.

Map<String, String> map = new HashMap<>();
map.put("amount", "1000000.23321312");

System.out.println(new BigDecimal(map.get("amount")).scale());

The displayed value is: 8

Buda Sergiu Flavius
  • 210
  • 1
  • 3
  • 13
0

Did you try a regular expression?

        double[] v = { 221, 22.1, 22.11 };
        Locale.setDefault(Locale.GERMANY);

        for (double val : v) {
            String str = String.format("%.2f", val);
            str = str.replaceAll("(.*?)0*$", "$1").replaceAll("(.*?)\\,$", "$1");

            System.out.println(str);
        }

  1. The first replaceAll removes trailing 0's
  2. The second replaceAll removes a trailing decimal point.
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I really need to set some time aside for regular expressions. It worked and nicely at that – Link Jan 07 '20 at 15:00