5

Is there a straightforward way to parse a string to either integer or float if I don't know if the variable is integer-number-like or decimal-number-like?

a = '2'; // => parse to integer
b = '2.1'; // => parse to float
c = '2.0'; // => parse to float
d = 'text'; // => don't parse

EDIT: It seems my question lacked the necessary context: I want to do some calculations without losing the original format (Original format thereby means integer vs. float. I don't care about the original number of decimal places):

Example:

String containing the formatted number ('2') => parse to number (2.0) => do some calculations (2.0 + 1 = 3.0) => restore "original format" ('3' and not '3.0')

If the input was 2.0 instead, the wanted result would be '3.0' (not '3').

am2124429
  • 425
  • 2
  • 6
  • 8
  • Why do you wan't to parse? Javascript has dynamic typing. Moreover, the only numerical type it has is double. – david a. Nov 05 '15 at 12:43
  • @david a.: Thanks for pointing that out. I wasn't aware of that. Still wondering why there are two functions (parseInt, parseFloat) in JavaScript if there is only one numerical type. – am2124429 Dec 01 '15 at 14:39
  • 1
    I think that the difference is just how the two parse the input - `parseInt()` only takes the integer part of it, while `parseFloat()` takes the decimal (and perhaps the exponent) as well. They, however, both return the same type (number, that is actually a floating-point type). See spec of JS numbers here: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-terms-and-definitions-number-value . MSDN mentions the same thing here: https://msdn.microsoft.com/library/7wkd9z69(v=vs.94).aspx#Anchor_4 – david a. Dec 01 '15 at 17:34
  • as there is no difference between a float and an int, you can really just do: `isNaN(val) ? val : parseFloat(val);` – Mortimer Aug 11 '17 at 05:55

4 Answers4

19

Multiply string which has number data with 1. You will get the Numeric data value.

var int_value = "string" * 1;

In your case

a = '2' * 1; // => parse to integer
b = '2.1' * 1; // => parse to float
c = '2.0' * 1; // => parse to float
d = 'text' * 1; // => don't parse    //NaN value

For last one you will get NaN value. Manually handle for NaN value

rajuGT
  • 6,224
  • 2
  • 26
  • 44
9

Just wrap it in Number()

Number('123') === 123

Number('-123.456') === -123.456

D G
  • 371
  • 4
  • 12
0

That's how I finally solved it. I didn't find any other solution than to add the variable type to the variable ...

var obj = {
 a: '2',
 b: '2.1',
 c: '2.0',
 d: 'text'
};
// Explicitly remember the variable type
for (key in obj) {
  var value = obj[key], type;
  if ( isNaN(value) || value === "" ) {
    type = "string";
  }
  else {
    if (value.indexOf(".") === -1) {
      type = "integer";
    }
    else {
      type = "float";
    }
    value = +value; // Convert string to number
  }
  obj[key] = {
    value: value,
    type: type
  };
}
document.write("<pre>" + JSON.stringify(obj, 0, 4) + "</pre>");
am2124429
  • 425
  • 2
  • 6
  • 8
0

You can use:

function parse(x){
  return x==x*1?x*1:x
 }

function parse(x){
  return x==x*1?x*1:x
 }
 
 console.log(parse(1),typeof parse(1))
 console.log(parse("1"),typeof parse("1"))
 console.log(parse("1.1"),typeof parse("1.1"))
 console.log(parse("1A"),typeof parse("1A"))
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48