2

I am trying to write a java function that will take a double as it's input, and return the double formatted as a string to the minimum number of decimal places required.

E.g.

Input:Result

50.000 : 50

12.500 : 12.5

74.125 : 74.125

Is there a suitable string format that I can use, or do I need to test the last character of the string to see if it's a 0? This would work, I'm sure, but seems a little agricultural.

I know there are a lot of questions on here about the reverse, and I apologise if this is a duplicate question - I did try to search!

  • it is indeed a duplicate of that question. Perfect answer. – Jonathan Windridge Jan 04 '14 at 22:48
  • @user2496329 hope you are not talking about accepted one if you can have values larger than max int/long – Pshemo Jan 04 '14 at 22:49
  • Shouldn't do - writing this for a workout-logging type program,(where I'm storing the weight as a double - correct me if this is wrong) so unless people start smashing out some *very* big records, I should be okay! – Jonathan Windridge Jan 04 '14 at 22:51

0 Answers0