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 › parseFloat for String vs String[]
Page Index Toggle Pages: 1
parseFloat for String vs String[] (Read 884 times)
parseFloat for String vs String[]
Sep 8th, 2008, 3:26pm
 
Can someone explain why parseFloat returns NaN for a single string, but 0.0 for arrays of strings, as in the following code? Many thanks.

String testString = "abc";
String[] testStringArray = { "abc", "def", "ghi" };

println (parseFloat(testString));
println (parseFloat(testStringArray));

gives me:

NaN
[0] 0.0
[1] 0.0
[2] 0.0

Re: parseFloat for String vs String[]
Reply #1 - Sep 8th, 2008, 6:35pm
 
Good question. It looks like a little inconsistency in the wrappers, unless there is some grand scheme that escapes me.
Note that in both cases, you can add a second parameter (float) giving the default value in case the string isn't a valid number.
Re: parseFloat for String vs String[]
Reply #2 - Sep 9th, 2008, 4:10am
 
yup, my mistake, it should return NaN.
Re: parseFloat for String vs String[]
Reply #3 - Sep 9th, 2008, 1:35pm
 
Thanks for the replies. It seemed like an inconsistency but I wasn't sure that it wasn't for a reason. Passing Float.NaN as the second parameter gets me the result I need.
Page Index Toggle Pages: 1