简体   繁体   English

在Java中如何将字符串解析为float并比较值?

[英]In Java how to parse a string to a float and compare the value?

I have a small code snippet as shown below, which as you can see has a hard-coded value for checking server version. 我有一个如下所示的小代码片段,如您所见,它具有用于检查服务器版本的硬编码值。 Now my intention is, if the server version is 11.3.0 or higher, then the if should be entered, but i am not able to figure out a way, Integer.parseInt won't work i guess as i parses int not float. 现在我的意图是,如果服务器版本是11.3.0或更高,那么应该输入if,但我无法找到方法, Integer.parseInt将无法正常工作,我想因为我解析int而不是浮点数。

String serverVersion = DatamodelVersion.getInstance().getVersion();
if(serverVersion.equalsIgnoreCase("11.3.0"))
{
    outstr = new FileOutputStream(confFile);
    prop.setProperty("NTFSDriver", "11.3.0/x86/tntfs.ko");
    prop.setProperty("NTFSDriver_x64", "11.3.0/x86_64/tntfs.ko");

    prop.store(outstr, "");

    update = true;
    System.out.println("Updated the tuxera conf file successfully");
    logger.logDebugAlways("Updated the tuxera conf file successfully");

}

Try this 尝试这个

String serverVersion = DatamodelVersion.getInstance().getVersion();
String[] version = serverVersion.split("\\.");
if (Integer.parseInt(version[0]) > 11 || (Integer.parseInt(version[0]) == 11 && Integer.parseInt(version[1]) >= 3))
{
    outstr = new FileOutputStream(confFile);
    prop.setProperty("NTFSDriver", "11.3.0/x86/tntfs.ko");
    prop.setProperty("NTFSDriver_x64", "11.3.0/x86_64/tntfs.ko");

    prop.store(outstr, "");

    update = true;
    System.out.println("Updated the tuxera conf file successfully");
    logger.logDebugAlways("Updated the tuxera conf file successfully");
}

there is not a built-in function in Java to transform 11.3.0 to float, because 11.3.0 is not a valid float number. Java中没有内置函数将11.3.0转换为float,因为11.3.0不是有效的float数。

for strings containing a valid float number, you could use Float.valueOf in Java. 对于包含有效浮点数的字符串,可以在Java中使用Float.valueOf

Split the version number by "." 将版本号拆分为“。”

Then compare one by one with your reference data. 然后逐一与参考数据进行比较。

String serverVersion = DatamodelVersion.getInstance().getVersion();
serverVersion.split('.')[0] // and so on..

A version number is neither an integer, nor a float. 版本号既不是整数,也不是浮点数。 Your best bet is using a specialized class: 你最好的选择是使用专门的课程:

public class Version implements Comparable<Version> {

  public Version(int major, int minor, int revision) {
    // set fields
  }

  public int compareTo(Version other) {
    // compare major minor and revision
  }

  public boolean equals(Object other) {
    // code here
  }      

  // parse a version in X.Y.Z form
  static Version parse(String version) {
    return new Version(//...);
  }
}

Using this you may decide to later add support for versions like 1.3.4-ALPHA or -RC1 and the like. 使用此功能,您可以决定稍后添加对1.3.4-ALPHA或-RC1等版本的支持。

You could try splitting the number into 3 parts, like so: 您可以尝试将数字拆分为3个部分,如下所示:

String[] bits = serverVersion.split(".");

Then, use a for loop and Integer.parseInt to parse each section of the number, and compare each. 然后,使用for循环和Integer.parseInt来解析数字的每个部分,并比较每个部分。

You need to define your own method for checking version numbers, based on the rules these numbers must follow in your domain. 您需要根据这些数字在您的域中必须遵循的规则来定义您自己的方法来检查版本号。

Based on the information you've provided, I would do a String split on . 根据您提供的信息,我会进行字符串拆分. and compare each value in turn (as an integer) against 11 , 3 and 0 respectively. 并依次每个值(作为一个整数)针对比较1130分别。

You have a Version class here : http://nvn.svn.sourceforge.net 你有一个Version类: http//nvn.svn.sourceforge.net

It should be used like this 它应该像这样使用

Version v = Version.parse(DatamodelVersion.getInstance().getVersion());

and store numbers in the standard format MAJOR.MINOR.BUILD.REVISION 并以标准格式MAJOR.MINOR.BUILD.REVISION存储号码

11.3.0 is not a float number. 11.3.0不是浮点数。 What about to use following code instead: 如何使用以下代码:

int Compare(String ver1, String ver2)
{
String[] ver1s = ver1.Split("\.");
String[] ver2s = ver2.Split("\.");
if(ver1s.length > ver2.length) return 1;
if(ver2s.length > ver1.length) return -1;
for(int i = 0;i < ver1s.length;i++)
{
   if(Integer.valueOf(ver1s[i]) > Integer.valueOf(ver2s[i])) return 1;
   if(Integer.valueOf(ver2s[i]) > Integer.valueOf(ver1s[i])) return -1;
}
return 0;
}

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

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