Sign in to follow this  
Followers 0
atrX

[Function] Converting a string to a floating point number

1 post in this topic

I got bored so decided to write some random functions and ended up writing something that might be somewhat useful to someone... It converts a string to a floating point number (or integer if it's not a decimal number). Have fun:



toFloat( in ) {
// Don't accept arrays or undefined, return 0
if( isArray( in ) || !isDefined( in ) )
return 0;

// Return original argument, it's not a string so doesn't need any special type conversion algorithm
if( !isString( in ) )
return in;

// Split string into 2 seperate strings
if( isSubStr( in, "," ) ) // Would be great if people wouldn't use fucking commas for decimals where I live
num = strTok( in, "," );
else
num = strTok( in, "." );

// Don't need to execute extra logic if the number isn't a decimal and therefore wasn't split into a multi-element array
if( num.size <= 1 )
return int( num[0] );

pwr = 10;
// Multiply by 10 for each extra character
// Initialize i as 1, we don't need to multiply on the first index
for( i = 1; i < num[1].size; i++ )
pwr *= 10;

return int( num[0] ) + int( num[1] ) / pwr;
}


 

Example usage:

number = toFloat( "666.66" ); // type = float, value = 666.66

4

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  
Followers 0