1

For example: 23.7748884 and 344.456445 are the numbers I am working with. I am looking for the output with the format "000.0000". The desired result would be 023.7749 and 344.4564. I tried:

String.format("%.4f", 23.7748884) // Output: 23.7749, NOT OK! Desired: 023.7749
String.format("%.4f", 344.456445) // Output 344.4564, OK!
Neil
  • 775
  • 1
  • 5
  • 19
  • 1
    Learn to use Google... http://stackoverflow.com/questions/275711/add-leading-zeroes-to-number-in-java – Benjamin M Mar 28 '15 at 17:00
  • Thanks. I did search, stumbled upon the same integer thread and I tried `String.format("%03.4f", 23.774884)`, however, I didn't quite understand the formatting arguments. – Neil Mar 28 '15 at 17:53

1 Answers1

3

You could reach such result using something like:

String.format("%08.4f", 23.7748884); // results 023.7749
String.format("%08.4f", 344.456445); // results 344.4564
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106