We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › converting a string into a number
Page Index Toggle Pages: 1
converting a string into a number (Read 1473 times)
converting a string into a number
Feb 24th, 2006, 9:02am
 
Hi,

I have dealt with this before in java but I can't seem to get it to work in processing.

I have:

String digitStr = "384762387642736842734"
int i = Integer.parseInt(digitStr);

I get an error that says Number Format Exception: for input string: "384762387642736842734"

What am I missing?

Thanks,

4dplane
Re: converting a string into a number
Reply #1 - Feb 24th, 2006, 9:45am
 
That number is bigger than what Integer
allows: -2147483648 to 2147483647.
The Integer is a signed 32-bit number.

.rex
Re: converting a string into a number
Reply #2 - Feb 24th, 2006, 5:38pm
 
in that case you'll have to use Long or Float:

Code:
long x=Long.parseLong("384762387642736842734"); // or
float x=Float.parseFloat("384762387642736842734");


hth!
Re: converting a string into a number
Reply #3 - Feb 24th, 2006, 9:15pm
 
Thanks for the help, but I am still having problems!

I cannot use the float because the number is not a decimial and when I try the long I get this error:

java.lang.NumberFormatException: For input string: "384762387642736842734"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)

etc ...


4dplane
Re: converting a string into a number
Reply #4 - Feb 25th, 2006, 11:09am
 
Unfortunately, your number is bigger than a long as well.

Long is limited to +/- 9223372036854775808(ish), and your number is about 40 times bigger than that.

You can treat it as a float/double without a decimal in it.(double is to float what long is to int). You'd lose some information, but you'd at least be able to load it, e.g.
Code:
String a="384762387642736842734" ;
float f=Float.parseFloat(a);
double d=Double.parseDouble(a);
println("float="+f);
println("double="+d);

Gives
Code:
float=3.847624E20
double=3.8476238764273684E20



                 
Page Index Toggle Pages: 1