Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test if a string is a valid float in JavaScript

I want to test if a string can be cast as a float. I have been trying to use parseFloat to achieve this.

console.log(!isNaN(parseFloat("10000"))); // should return true
console.log(!isNaN(parseFloat("100T0"))); // should return false (but doesn't)

Turns out that parseFloat just stops reading a string as soon as it hits a character that's not a digit, then returns whatever it has passed over so far. So parseFloat("100T0") actually returns 100 and not NaN.

What's a better js test to see if a string can be cast as a float?

like image 692
rvictordelta Avatar asked Oct 10 '18 12:10

rvictordelta


People also ask

How do you check if a string is a float JavaScript?

Check if the value has a type of number and is not an integer. Check if the value is not NaN . If a value is a number, is not NaN and is not an integer, then it's a float.

How do I check if a string has a float?

To check if a string is an integer or a float:Use the str. isdigit() method to check if every character in the string is a digit. If the method returns True , the string is an integer. If the method returns False , the string is a floating-point number.

How do you check whether a string is integer or float in JavaScript?

isInteger() In the above program, the passed value is checked if it is an integer value or a float value. The typeof operator is used to check the data type of the passed value. The isNaN() method checks if the passed value is a number.

How check string is valid or not in JavaScript?

To check if a string is a valid number: Check that the string is not an empty string or contains only spaces. Pass the string to the isNaN() function. If isNaN returns false , the string is a valid number.


2 Answers

There's no one built-in operation provided by default in JavaScript that matches a reasonable "is this string a number" definition (to my mind, anyway). You can get close with Number, unary +, or implicit conversion (just passing the string into isNaN directly), with the caveat that they all do the same thing, which includes considering "" to be 0:

// Number
console.log(!isNaN(Number("10000"))); // true
console.log(!isNaN(Number("100T0"))); // false
console.log(!isNaN(Number("")));      // true (!)

// Same as implicit (here triggered with a unary +)
console.log(!isNaN(+"10000")); // true
console.log(!isNaN(+"100T0")); // false
console.log(!isNaN(+""));      // true (!)


// Same as implicit
console.log(!isNaN("10000")); // true
console.log(!isNaN("100T0")); // false
console.log(!isNaN(""));      // true (!)

My answer to a related question goes into your options in detail.

Consequently, you can either do a regular expression (be sure to allow for scientific notation!) or a check for "" up-front:

function toNumber(str) {
  str = String(str).trim();
  return !str ? NaN : Number(str);
}
console.log(!isNaN(toNumber("10000"))); // true
console.log(!isNaN(toNumber("100T0"))); // false
console.log(!isNaN(toNumber("")));      // false
like image 66
T.J. Crowder Avatar answered Sep 20 '22 07:09

T.J. Crowder


You won't get what you expect with parseFloat(), because it tries to parse 100T0 and returns 100, so it's a valid Number.

Instead of using isNaN() you can just use regex, to check if the given string is only composed of numbers.

This is the regex /^\-?[0-9]+(e[0-9]+)?(\.[0-9]+)?$/ you need, and this is how should be your code:

function isItNumber(str) {
  return /^\-?[0-9]+(e[0-9]+)?(\.[0-9]+)?$/.test(str);
}

Demo:

function isItNumber(str) {
  return /^\-?[0-9]+(e[0-9]+)?(\.[0-9]+)?$/.test(str);
}

console.log(isItNumber("10000")) // should return true
console.log(isItNumber("100T0"))
console.log(isItNumber("10e5"))
console.log(isItNumber("100.50"))
like image 23
cнŝdk Avatar answered Sep 21 '22 07:09

cнŝdk