简体   繁体   English

在JOptionPane中格式化字符串值

[英]Formatting a string value within a JOptionPane

I want to format a string, so instead of the value reading: 1.599999, it reads 1.59. 我想格式化一个字符串,所以它的读数不是1.599999,而是1.59。 For the rest of my program, I've been using: %.2f to format the JLabels, but when I try do that in a JOptionPane, it doesn't recognize it as format code, but as text. 对于程序的其余部分,我一直在使用:%.2f格式化JLabel,但是当我尝试在JOptionPane中进行格式化时,它不会将其识别为格式代码,而是文本。

EDIT: 编辑:

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

Split it out into a String variable and put it inline (I avoid putting it directly inline for smaller lines of code and easier debugging, personal preference though): 将其拆分为一个String变量并内联(我避免将其直接内联用于较小的代码行,并且更容易调试和个人喜好):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM