0

I want to format a double to string with the max length is 7 which contains a dot "." and one digit after it.
For example:

 123.4 becomes "00123.4"
 12345 becomes "12345.0"
 12345.63 becomes "12345.6"

Any help, please!

Ngo Van
  • 857
  • 1
  • 21
  • 37
  • possible duplicate of [How do I format a number in java?](http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java) – alex Apr 03 '14 at 11:23
  • possible duplicate of [Fixed decimal numbers with JAVA](http://stackoverflow.com/questions/4377209/fixed-decimal-numbers-with-java) – Raedwald Apr 03 '14 at 11:44

3 Answers3

3

you can do this:

double test = 33333.327;

String formatted = String.format("%07.1f", test)

System.out.println(formatted);
morgano
  • 17,210
  • 10
  • 45
  • 56
0

Try this -

DecimalFormat df = new DecimalFormat("00000.0");

...

System.out.println(df.format(123.4)); -> 00123.4
System.out.println(df.format(12345)); -> 12345.0
System.out.println(df.format(12345.63)); -> 12345.6
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0
String.format("%07.1f", myDouble);

See http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

wggn
  • 77
  • 6