Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串转换为int格式_Java - Fatal编程技术网

Java 将字符串转换为int格式

Java 将字符串转换为int格式,java,Java,我有这个数字3.445,我需要把它转换成int,这样我就可以把它写入postgre数据库。这是我的代码: NumberFormat numberFormat = NumberFormat.getInstance(Locale.UK); numberOfEmployees = (Integer) numberFormat.parse(numberOfEmployeesString); 注意:这个数字是三千四百四十四。以下应该可以 int i = numberOfEmployees != nul

我有这个数字3.445,我需要把它转换成int,这样我就可以把它写入postgre数据库。这是我的代码:

NumberFormat numberFormat = NumberFormat.getInstance(Locale.UK);
numberOfEmployees = (Integer) numberFormat.parse(numberOfEmployeesString); 

注意:这个数字是三千四百四十四。

以下应该可以

int i = numberOfEmployees != null ? numberOfEmployees.intValue() : 0;
您可以将其转换为字符串,删除“.”(我首先误读了它),然后使用Integer.parseInt从字符串中获取int值
但是,这一切都取决于您是否能够确定值始终具有该格式。

3.445不是您用文字描述的数字。它给了您什么?数字没有格式。若要保留格式,可以将其保留为字符串。@Faruk请放心,我正试图提供帮助。我可能会误解你的问题,请纠正我。当需要编辑时,我会编辑。@Faruk现在我明白了。误会事物不是犯罪,这就是为什么我们互相询问和回答。好的,我现在知道答案了。你能告诉我为什么不起作用吗:NumberFormat NumberFormat=NumberFormat.getInstance(Locale.derman);numberOfEmployees=(整数)numberFormat.parse(numberOfEmployeesString);因为您将得到这样一个异常:
java.lang.Long不能转换为java.lang.Integer
  String bd = "3.445";///this is the string which you have 

  ////If you want the grouping separator to be a point, you can use an european locale:
  Number parse = NumberFormat.getNumberInstance(java.util.Locale.GERMAN).parse(bd);

  int n=parse.intValue();/// this is the integer value for your string