[英]How to parse string to int?
my string is 743.4445 and I want it to show 743 so it has to parse to double and then parse to int so I try like this我的字符串是 743.4445 我希望它显示 743 所以它必须解析为 double 然后解析为 int 所以我尝试这样
(int)(Double.valueOf(743.4445);
(actually 743.4445 is from server thus I don't know exactly value) (实际上 743.4445 来自服务器,因此我不知道确切的值)
what am I suppose to do?我该怎么办?
why not google "java get whole number"为什么不谷歌“java获取整数”
double d = Double.valueOf(s.trim()).doubleValue();
Heck, for that matter why not use IndexOf on the string looking for the.?哎呀,为什么不在字符串上使用 IndexOf 来寻找。? :) :)
Please try this.请试试这个。
Store this value in one string and try this.将此值存储在一个字符串中并尝试此操作。
String number = "743.4445";字符串编号 = "743.4445";
(int)Double.parseDouble(number)
Why not use NumberFormat.setParseIntegerOnly(boolean value)
method?为什么不使用NumberFormat.setParseIntegerOnly(boolean value)
方法?
Example:例子:
String s = "743.5555";
NumberFormat format = NumberFormat.getInstance();
format.setParseIntegerOnly (true);
int value = format.parse(s).intValue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.