else if (bb.equals("Dollar") && cc.equals("Pound")){
input = JOptionPane.showInputDialog("What amount would you like to convert?:");
double aa = Double.parseDouble(input);
double output =(aa*rate);
JOptionPane.showMessageDialog(null, String.format("Your amount is: $%.2f", output));
如何做到这一点,如果有人输入负数,会弹出一条消息说无效输入?
答案 0 :(得分:0)
我假设您要检查的数字是否由变量aa
定义为正数?我个人更喜欢在处理货币价值时使用BigDecimal
,在这种情况下我会这样做:
BigDecimal aa = null;
try {
aa = new BigDecimal(input);
} catch (NumberFormatException nfe) {
//Not a numerical value, throw an error
}
if (aa.compareTo(BigDecimal.ZERO) <= 0) {
//Value is negative
}