将包含十进制的字符串转换为Long。

时间:2022-01-15 17:09:08

I have following sample (link to ideone).

我有以下样本(链接到ideone)。

long lDurationMillis =  0;
lDurationMillis = Long.parseLong("30000.1");
System.out.print("Play Duration:" + lDurationMillis);

It throws an exception:

它将抛出一个异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: "30000.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:419)
at java.lang.Long.parseLong(Long.java:468)
at Main.main(Main.java:9)

But why it wont let me convert that number to a string directly ?I can convert number to integer and than convert to double . But is there any other way ?

但是为什么它不让我直接把这个数字转换成一个字符串呢?我可以把数字转换成整数,而不是转换成double。但是还有别的办法吗?

5 个解决方案

#1


29  

The value 30000.1 is an invalid long value. You could parse the double value first:

值30000.1是无效的长值。你可以先解析双重值:

lDurationMillis = (long)Double.parseDouble("30000.1");

#2


4  

The title says converting string to long, first question is about coverting number to string, next statement about converting number to integer to string. I am confuse.

题目说把字符串转换成长,第一个问题是关于把数字转换成字符串,第二个问题是关于把数字转换成整数变成字符串。我混淆了。

But for anything to do with floating points, I have to point you at obligatory reference What Every Computer Scientist Should Know About Floating-Point Arithmetic .

但是,对于任何与浮点运算有关的东西,我都必须指出,对于浮点运算,每个计算机科学家都应该知道些什么。

In java, int and long do not have fractional parts, so a string like 3000.1 cannot be covnerted to one of these. It can be converted to float or double but if you read the above article you will realize that the coversion can be lossy, i.e. if you canvert that double back to a String you may not get the original 3000.1 back. It will be something close, for appropriate defintion of close, but may not be same.

在java中,int和long没有小数部分,所以像3000.1这样的字符串不能编码到其中之一。它可以转换为浮点数或双精度数,但如果你读了上面的文章,你会发现覆盖可以是有损的,也就是说,如果你把那个双精度的数据转换回一个字符串,你可能无法得到原来的3000.1。它将是接近的东西,为了恰当地定义接近,但可能不一样。

If you want to use exact precision then BigDecimal is your friend. It will be much slower then the number types, but it will be precise.

如果你想使用精确的精度,那么BigDecimal是你的朋友。它将比数字类型慢得多,但它将是精确的。

#3


2  

Because long can't have fractional part, you could convert it to double and then cast it to long ignoring fractional part

因为long不能有小数部分,你可以把它转换成double然后把它转换成long忽略小数部分

#4


2  

You could use BigDecimal in this case:

在这种情况下可以使用BigDecimal:

BigDecimal bd = new BigDecimal("30000.1");
long l = bd.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
System.out.println(l);

#5


1  

You can you NumberFormat i.e.

你可以用NumberFormat来表示。

long lDurationMillis =  0;
        try{
            NumberFormat nf = NumberFormat.getInstance();
            lDurationMillis = nf.parse("30000.1").longValue();
            System.out.print("Play Duration:" + lDurationMillis);
        }catch(ParseException e)
        {
            e.printStackTrace();
        }

Output:

输出:

Play Duration:30000

#1


29  

The value 30000.1 is an invalid long value. You could parse the double value first:

值30000.1是无效的长值。你可以先解析双重值:

lDurationMillis = (long)Double.parseDouble("30000.1");

#2


4  

The title says converting string to long, first question is about coverting number to string, next statement about converting number to integer to string. I am confuse.

题目说把字符串转换成长,第一个问题是关于把数字转换成字符串,第二个问题是关于把数字转换成整数变成字符串。我混淆了。

But for anything to do with floating points, I have to point you at obligatory reference What Every Computer Scientist Should Know About Floating-Point Arithmetic .

但是,对于任何与浮点运算有关的东西,我都必须指出,对于浮点运算,每个计算机科学家都应该知道些什么。

In java, int and long do not have fractional parts, so a string like 3000.1 cannot be covnerted to one of these. It can be converted to float or double but if you read the above article you will realize that the coversion can be lossy, i.e. if you canvert that double back to a String you may not get the original 3000.1 back. It will be something close, for appropriate defintion of close, but may not be same.

在java中,int和long没有小数部分,所以像3000.1这样的字符串不能编码到其中之一。它可以转换为浮点数或双精度数,但如果你读了上面的文章,你会发现覆盖可以是有损的,也就是说,如果你把那个双精度的数据转换回一个字符串,你可能无法得到原来的3000.1。它将是接近的东西,为了恰当地定义接近,但可能不一样。

If you want to use exact precision then BigDecimal is your friend. It will be much slower then the number types, but it will be precise.

如果你想使用精确的精度,那么BigDecimal是你的朋友。它将比数字类型慢得多,但它将是精确的。

#3


2  

Because long can't have fractional part, you could convert it to double and then cast it to long ignoring fractional part

因为long不能有小数部分,你可以把它转换成double然后把它转换成long忽略小数部分

#4


2  

You could use BigDecimal in this case:

在这种情况下可以使用BigDecimal:

BigDecimal bd = new BigDecimal("30000.1");
long l = bd.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
System.out.println(l);

#5


1  

You can you NumberFormat i.e.

你可以用NumberFormat来表示。

long lDurationMillis =  0;
        try{
            NumberFormat nf = NumberFormat.getInstance();
            lDurationMillis = nf.parse("30000.1").longValue();
            System.out.print("Play Duration:" + lDurationMillis);
        }catch(ParseException e)
        {
            e.printStackTrace();
        }

Output:

输出:

Play Duration:30000