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 float
Page Index Toggle Pages: 1
converting a string into a float? (Read 4010 times)
converting a string into a float?
Jan 28th, 2009, 12:24pm
 
does anyone know how to convert a string variable into a float. my programme creates a string to read text files but i want to use that data from the string into a float so that i can use it on screen

Re: converting a string into a float?
Reply #1 - Jan 28th, 2009, 1:19pm
 
Code:

float f = Float.valueOf(str).floatValue();
Re: converting a string into a float?
Reply #2 - Jan 28th, 2009, 2:49pm
 
Or just:

float f = Float.parseFloat(str);

Note it can throw a NumberFormatException if the string isn't a real float (pun intended...).

A simpler, more Processing way is to just use float()! Smiley
Re: converting a string into a float?
Reply #3 - Feb 9th, 2009, 12:18pm
 
Or float f = new Float(str);

In an external editor (like Eclipse), we cannot use float(str)... (as it causes Syntax error)
Re: converting a string into a float?
Reply #4 - Feb 9th, 2009, 1:33pm
 
Ah, yes, because it is a Java keyword.
I think it is a trick of the pre-processor.

That said, I would still use my static function, avoiding creating an object just to convert it back to the primitive type.
Page Index Toggle Pages: 1