Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to parseFloat the whole string?

As you know, the javascript's parseFloat function works only until it meets an invalid character, so for example

parseFloat("10.123") = 10.123
parseFloat("12=zzzz") = 12
parseFloat("z12") = NaN

Is there a way or an implementation of parseFloat that would return NaN if the whole string is not a valid float number?

like image 289
mcm69 Avatar asked Jul 15 '10 15:07

mcm69


2 Answers

Use this instead:

var num = Number(value);

Then you can do:

if (isNaN(num)) {
    // take proper action
}
like image 124
dcp Avatar answered Oct 13 '22 00:10

dcp


Maybe try:

var f = parseFloat( someStr );
if( f.toString() != someStr ) {
  // string has other stuff besides the number
}

Update: Don't do this, use @dcp's method :)

like image 34
rfunduk Avatar answered Oct 13 '22 00:10

rfunduk