-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"
Chris Chambers
  • 1,367
  • 21
  • 39
motion
  • 21
  • 4
  • 2
    Have you looked at [`String.format()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.util.Locale,%20java.lang.String,%20java.lang.Object...)) and [format strings](http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax)? – thegrinner May 28 '13 at 17:19

1 Answers1

2

I think this solution is cleaner:

DecimalFormat df = new DecimalFormat("+0.##############");
df.format(1.5);
Robe Elckers
  • 967
  • 1
  • 6
  • 19