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?

1

1 Answer 1

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.#"); 
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.