在JOptionPane中格式化字符串值

时间:2015-03-18 15:50:27

标签: java text string-formatting joptionpane

我想格式化一个字符串,所以不是读取值:1.599999,而是读取1.59。对于我的程序的其余部分,我一直在使用:%。2f来格式化JLabel,但是当我尝试在JOptionPane中执行此操作时,它不会将其识别为格式代码,而是将其识别为文本。

编辑:

  amountEntered = JOptionPane.showInputDialog(finishPayInput, "Please enter the full total of: £%.2f" + convPrice);

2 个答案:

答案 0 :(得分:1)

amountEntered = JOptionPane.showInputDialog(finishPayInput,
    String.format("Please enter the full total of: £%.2f", convPrice))

答案 1 :(得分:1)

将其拆分为String变量并将其内联(我避免将其直接内联以获得较小的代码行,并且更容易调试,但个人偏好):

String fullTotal = String.format("Please enter the full total of: £%.2f", convPrice);
amountEntered = JOptionPane.showInputDialog(finishPayInput, fullTotal);