简体   繁体   English

如何在Java字符串中定义是否大于

[英]How to define if greater then in java string

In the following method I need to define that only set the value if its not null and if the value is greater than "0.00" How do I define that. 在以下方法中,我需要定义仅在不为null且值大于“ 0.00”的情况下设置该值的方法。

 String postagePaid1 = trackInfo.getPostage();
                    if(postagePaid1 != null && ????) {
                        claim.setClPtsPostageFee(new BigDecimal(postagePaid1));
                    }

You can't do numeric comparisons with Strings as it doesn't make sense in the String world, only with numbers. 您无法使用String进行数字比较,因为在String世界中,仅使用数字是没有意义的。 So first check that it's not null, and if not, convert it to a double via Double.parseDouble(...) 因此,首先检查它是否不为null,如果不是,则通过Double.parseDouble(...)将其转换为double Double.parseDouble(...)

if (someStr != null && !someStr.trim().isEmpty()) {
  try {
    double someNumber = Double.parseDouble(someStr.trim());
    if (someNumber > 0.0) {
       // ***** here you are ****
    }
  } catch (NumberFormatException nfe) { 
      // some action if String is not numeric
  }
}

Don't use String values as doubles during comparison. 在比较期间不要将String值用作双精度值。 String comparison is done in natural order , so "1111" will be lesser than "9" . 字符串比较以自然顺序完成,因此"1111"小于"9"

In your case, use Double.parseDouble(doubleString) and then compare values. 在您的情况下,使用Double.parseDouble(doubleString) ,然后比较值。

if postagePaid1 contains only double type value ,you have to convert the string to double type by using following code in order to be able to do comparison 如果postagePaid1仅包含double类型值,则必须使用以下代码将字符串转换为double类型,以便能够进行比较

Double.parseDouble(postagePaid1)

so if statement become : 所以如果语句变为:

 if(postagePaid1 != null && Double.parseDouble(postagePaid1)>0.00) {

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

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