24

I have following sample (link to 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 ?

5
  • Why not convert the decimal directly to a double with Double.parseDouble(text); or to a BigDecimal with new BigDecimal(text); Oct 4, 2012 at 17:14
  • 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 Oct 4, 2012 at 17:17
  • Not clear what do you mean. You CAN convert any number to String with, for instance, Long.toString(long l).
    – Suzan Cioc
    Oct 4, 2012 at 17:19
  • Long.toString(long l) also gives the same error
    – Swamy
    Jul 8, 2013 at 11:11
  • are you aware of the fact that 30000.1 is not a valid long?
    – ACV
    Jan 13, 2017 at 10:55

5 Answers 5

51

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

lDurationMillis = (long)Double.parseDouble("30000.1");
2
  • 3
    I've read that using the double data type may lead to precise errors. Couldn't that happen here?
    – Patrick
    Aug 25, 2017 at 1:33
  • 2
    This may only occur in the fractional part of the number. The integral part will be fine :)
    – Reimeus
    Mar 22, 2018 at 17:09
7

You could use BigDecimal in this case:

BigDecimal bd = new BigDecimal("30000.1");
long l = bd.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
System.out.println(l);
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.

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.

2

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

2

You can do NumberFormat handling as below :

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.