0

The following Code

private double roundTheReading(double toRoundValue) {
    double roundetValue = 0;
    if (formatter == null){
    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
    dfs.setDecimalSeparator('.');
    formatter = new DecimalFormat(("#0." + zerosAfterDot),dfs);
    }
    roundetValue = Double.parseDouble(formatter.format(toRoundValue));
    return roundetValue;
}

makes a shorting of the double "toRoundValue" because it haves 9 after dot characters and i need only 0, 1 or 2 of them (is set by zerosAfterDot by an Input Method).

Problem is: if zerosAfterDot is "0" the answer should be 5 or 3421. I get the . and 1 Zero everytime (5.0 3421.0).

Dose this have something to do with the Line:

formatter = new DecimalFormat(("#0." + zerosAfterDot),dfs);

Is the dot in this line the dot i get in my output? If so i could make an If() what checks if the value fter the dot is zero. But i would rather not do that because it would also kill the Dot if the zerosAfterDot > 0. And i want to get around to many if statements.

TL,DR How do i get rid of the "." when i dont have numbers following it that are significant?

Fulli
  • 117
  • 5
  • possible duplicate of [How to nicely format floating numbers to String without unnecessary decimal 0?](http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0) – icza Sep 05 '14 at 10:11

1 Answers1

0

You're probably displaying the double value returned from roundTheReading which will always include non-significant digits. Instead you can return a String and use

formatter = new DecimalFormat(("0.#");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • The value is used in other ways to, so an String return is not the best way because i need to typecast it back everytime i want to use it. But i can give the UI the string in the getter Method. its a simple answer i didnt think of until i saw yours. Thanks for that! – Fulli Sep 05 '14 at 10:23