为什么表示数字的String的Integer.parseInt会在J2ME中抛出java.lang.NumberFormatException? [英] Why Integer.parseInt of a String representing a number throws java.lang.NumberFormatException in J2ME?
问题描述
我想总结一个记录库
的四列的值。这四列中的值是小数点
的数字,例如 30.00
; recordstore
行是一个 csv-like
数据,我使用userdefined函数获取特定列的值:所以我得到了四列的 String
,例如30.00
。现在我要 sum
这四个值;所以我必须将
转换为 int
!但是当我尝试使用 Integer.parseInt
时,会引发 java.lang.NumberFormatException
!那么如何在这种情况下得到总和?
I want to sum the values of four columns of a recordstore
. The values in these four columns are numbers with the decimal point
, for example 30.00
; the recordstore
row is a csv-like
data with which I use a userdefined function to get the value of a particular column: so I get a String
for the four columns , for example "30.00"
. Now I want to sum
these four values ; so I must convert
them into int
! But when I attempt to use Integer.parseInt
then the java.lang.NumberFormatException
is raised ! So how to make the sum in this situation ?
推荐答案
即使 30.00
看起来像是一个整数,Java认为它看起来像一个浮点值(由于小数点)。
Even though 30.00
seems like an integer to you, Java thinks it looks like a floating point value (due to the decimal point).
因此你需要将它解析为double,然后得到整数部分。
You therefore need to parse it as a double, and then get the integer part.
int i = Double.parseDouble("30.00").intValue();
(顺便说一句,不是特定于J2ME的。)
(Not J2ME specific by the way.)
这篇关于为什么表示数字的String的Integer.parseInt会在J2ME中抛出java.lang.NumberFormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!