[英]how to compare a Double Value in if statement in java?
I have a desktop application with textboxs that contain a price values.我有一个带有包含价格值的文本框的桌面应用程序。 As an example if i input 25000
例如,如果我输入 25000
Double priceSec = Double.parseDouble(cusPrice.getText());
i'm passing that 25000 as a double value我将 25000 作为双值传递
so now i want to compare that value and check whether it is empty or not?所以现在我想比较该值并检查它是否为空?
if(cusPrice2.getText()==null || cusPrice2.getText().isEmpty()){
Double priceSec=0.0;
}
i know i can create an else condition for我知道我可以创建一个 else 条件
if textbox !=null
total=txtbox1+txtbox2+txtbox3;
if textbox value is empty or not empty.如果文本框值为空或不为空。 value should be there in total.
总价值应该在那里。 but in my code ,it displays as null.
但在我的代码中,它显示为空。
Do i have any other way to do that?我还有其他方法可以做到这一点吗? can you tell me a way to do that.
你能告诉我一种方法吗?
here is my full code这是我的完整代码
private void cusOkBtnActionPerformed(java.awt.event.ActionEvent evt) {
Double priceSec = Double.parseDouble(cusPrice2.getText());
Double priceThird = Double.parseDouble(cusPrice3.getText());
Double priceFourth = Double.parseDouble(cusPrice4.getText());
Double priceFifth = Double.parseDouble(cusPrice5.getText());
if(cusPrice2.getText()==null || cusPrice2.getText().isEmpty() || cusPrice2.getText() !=null ){
priceSec=0.0;
costCls.setItemPrice(priceSec);
}
else if(cusPrice3.getText()==null || cusPrice3.getText().isEmpty()){
priceThird=0.0;
}
else if(cusPrice4.getText()==null || cusPrice4.getText().isEmpty()){
priceFourth=0.0;
}
else if(cusPrice5.getText()==null || cusPrice5.getText().isEmpty()){
priceFifth=0.0;
}
Double setItemTotal = priceCus+priceSec+priceThird+priceFourth+priceFifth;
}
You should rather use Exception
s.您应该使用
Exception
。
try {
Double priceSec = Double.parseDouble(cusPrice.getText());
} catch (NullPointerException e1) {
//null somewhere
} catch (NumberFormatException e2) {
//not a number - on controlled input - empty field.
}
It is better to use the built-in Exception
semantics already available within the Double.parseDouble(String s)
implementation.最好使用
Double.parseDouble(String s)
实现中已经可用的内置Exception
语义。 (IMHO: Don't check up-front as you are making the exceptional-situation part of the normal flow) (恕我直言:不要预先检查,因为您正在将异常情况作为正常流程的一部分)
Use a function like使用类似的功能
private static double getValue(String str) {
try {
return Double.parseDouble(str);
} catch (NumberFormatException e) {
return 0d;
}
}
You would then use it like:然后你会像这样使用它:
String priceStr = cusPrice.getText();
double d = getValue(priceStr);
double
is the primitive type for floating point, Double
is the Object child that wraps a double value. double
是浮点的原始类型, Double
是包装 double 值的 Object 子级。 So:所以:
double priceSec = Double.parseDouble(cusPrice.getText());
double price2Sec = cusPrice2.getText().isEmpty())
? 0.0
: Double.parseDouble(cusPrice.getText());
if (priceSec == price2Sec) { ...
However for financial data a double has small approximation errors, that can become visible - especially with ==
.然而,对于财务数据,double 具有很小的近似误差,这可能会变得可见 - 特别是使用
==
。 Hence one might better use BigDecimal, which does fixed point arithmetic.因此,最好使用 BigDecimal,它执行定点运算。
BigDecimal priceSec = new BigDecimal(cusPrice.getText());
if (priceSec.compareTo(price2Sec) == 0) { ...
With a bit ugly API doing calculations:用一个有点丑陋的 API 做计算:
x = x.add(y.divide(new BigDecimal("12.50")));
you can try this你可以试试这个
private void cusOkBtnActionPerformed(java.awt.event.ActionEvent evt) {
Double priceSec=0.0,priceThird=0.0,priceFourth=0.0,priceFifth=0.0;
costCls.setItemPrice(0.0);
if(cusPrice2.getText()!=null && !cusPrice2.getText().isEmpty() ){
priceSec = Double.parseDouble(cusPrice2.getText());
costCls.setItemPrice(priceSec);
}
else if(cusPrice3.getText()!=null && !cusPrice3.getText().isEmpty()){
priceThird = Double.parseDouble(cusPrice3.getText());
}
else if(cusPrice4.getText()!=null && !cusPrice4.getText().isEmpty()){
priceFourth = Double.parseDouble(cusPrice4.getText());
}
else if(cusPrice5.getText()!=null && !cusPrice5.getText().isEmpty()){
priceFifth = Double.parseDouble(cusPrice5.getText());
}
Double setItemTotal = priceCus+priceSec+priceThird+priceFourth+priceFifth;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.