Converting a String that contains decimal to Long

后端 未结 5 1439
庸人自扰
庸人自扰 2020-12-11 15:00

I have following sample (link to ideone).

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


        
5条回答
  •  轮回少年
    2020-12-11 15:33

    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.

    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.

提交回复
热议问题