Double decimal formatting in Java

后端 未结 14 1249
囚心锁ツ
囚心锁ツ 2020-11-22 05:17

I\'m having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it\'s 4.00 instead?

14条回答
  •  别那么骄傲
    2020-11-22 05:40

    Using String.format, you can do this:

    double price = 52000;
    String.format("$%,.2f", price);
    

    Notice the comma which makes this different from @Vincent's answer

    Output:

    $52,000.00

    A good resource for formatting is the official java page on the subject

提交回复
热议问题