-3

I need to format a double value to string in such a way that it has a sign in front ("+" whether it's positive, "-" whether it's negative) and has no trailing zeros. For example:

Input: 1.5
Output: "+1.5"

Input: 1.0
Output: "+1"

Input: -123.123321
Output: "-123.123321"
1

1 Answer 1

2

I think this solution is cleaner:

DecimalFormat df = new DecimalFormat("+0.##############");
df.format(1.5);
Sign up to request clarification or add additional context in comments.

1 Comment

DecimalFormat df = new DecimalFormat("+0.####;-#");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.