-1

I have troubles with a variable in JavaScript that I want to be int. In the beginning, it looked like an int, then I did some diagnostics and found out it is not. Ultimately, I want to cast it to int.

This is the code I used:

console.log(variable);
var isInt = variable % 1 === 0;
console.log('The variable is int?');
console.log(isInt);
var isFloat = +variable && variable !== (variable|0);
console.log('The variable is float?');
console.log(isFloat);

And these are the results:

2,365
The variable is int?
false
The variable is float?
NaN

The line for checking if the variable is float I got it from this question: How do I check that a number is float or integer? It is the second answer.

NaN means my variable is probably a string?

EDIT: My question is different from the one offered as a solution, as i am not sure if my variable is float or integer, thus i am trying to identify it first. I picked parts from answers from that question, but it did nto work.

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • You know that Number.isInteger exists right? – Ertan Kara Oct 03 '18 at 11:29
  • @ErtanKara yeah. The linked question not only has an answer that says that but also offers the polyfill, so it can be implemented even if the environment doesn't support it yet. – VLAZ Oct 03 '18 at 11:29
  • you can simply log typeof variable to check the type – SamGhatak Oct 03 '18 at 11:30
  • 1
    Possible duplicate of [How do I check that a number is float or integer?](https://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-float-or-integer) – VLAZ Oct 03 '18 at 11:30
  • 2
    `2,365` is a string. `2.365` could be float or string. –  Oct 03 '18 at 11:30
  • good observation – SamGhatak Oct 03 '18 at 11:31
  • NaN means "not a number". +variable casts it to number, and as ChrisG said, it's a string with comma. – Sergio Tx Oct 03 '18 at 11:31
  • @SamGhatak thanks! I didn't know that. Turns out it is a string. – user1584421 Oct 03 '18 at 11:43
  • Concerning your edit: the question dupe STILL HAS AN ANSWER FOR YOU. The answer you picked explicitly only checks variables that are already numeric. The accepted answer already has a solution for an unknown type of variable. There is another solution that suggests using built in (ES6) checks and provides ES5 code that achieves exactly the same. – VLAZ Oct 03 '18 at 11:44
  • What is the correct thing to do then, for my situation? Should i accept the other answer and mark mine as duplicate? – user1584421 Oct 03 '18 at 11:45
  • Well, I don't know. Everything you've said so far makes it seem like it's a complete duplicate. You need to know if a variable is numeric and which type or a string representation of a JS numeric type. Both of those have answers in the question you linked. Unless you're asking something different, I would consider it a dupe. – VLAZ Oct 03 '18 at 11:50
  • Now there is no button to mark it as duplicate. – user1584421 Oct 03 '18 at 11:53
  • @vlaz While I tend to agree, this question is about parsing a string that uses a comma as decimal symbol. So strictly speaking not a dupe. –  Oct 03 '18 at 12:00
  • @ChrisG Maybe i should edit the title or the question? – user1584421 Oct 03 '18 at 12:12
  • @user1584421 I don't know, maybe. The problem is that your question is essentially two separate questions due to the `,` with the second being about turning a float to an int, not so much detecting what it is. It's almost an XY problem, and everybody is only focusing on the Y part. –  Oct 03 '18 at 12:21
  • Is you actual question something like: I'm getting this: `{ "value": "2,365" }` and I want to turn `value` into an int? –  Oct 03 '18 at 12:23
  • @ChrisG I guess it is.. But until i arrived in that conclusion, i had to first pass through the mental space of "i have to identify what this variable is"... – user1584421 Oct 03 '18 at 12:40

2 Answers2

2

Note: parseInt() will cut off any and all decimals!

If you're getting "numbers" (strings) with , as decimal symbol, and you want to turn them into integers by rounding, you can use this:

function toInt(x) {
  if (!isNaN(x)) return Math.round(x);
  if (typeof x == "string") x = x.replace(",", ".");
  return Math.round(parseFloat(x));
}

console.log(toInt(5));
console.log(toInt(5.5));
console.log(toInt("5"));
console.log(toInt("5.5"));
console.log(toInt("5,5"));

Shorter version:

const toInt = x => Math.round(isNaN(x) ? (typeof x == "string" ? x.replace(",", ".") : x) : x);
  • To do the replace, you have to be sure that the commas are fraction separators. They could separate thousands in some cultures, so `"1,000,000"` represents one *million* not simply *one* which the function returns. – VLAZ Oct 03 '18 at 11:47
  • Hmmm... I tried your short version but it did not work. After i run the command, i typed console.log(typeof variable); and i get "function". – user1584421 Oct 03 '18 at 11:48
  • 1
    Then tried this: "variable = parseInt(variable);". And it worked! – user1584421 Oct 03 '18 at 11:56
  • 1
    Yep, you need to use the return value. Wait, what, `parseInt`? Mine works fine: https://jsfiddle.net/khrismuc/eakfpcyq/ (mine rounds the value, parseInt cuts off decimals!) –  Oct 03 '18 at 11:56
  • I will do further testing and i will post here the results later. Thanks! – user1584421 Oct 03 '18 at 12:41
0

in the first line try

console.log(typeof variable)

and this will tell you the type of the variable is string or not which might be causing NaN in the last check

ashish singh
  • 6,526
  • 2
  • 15
  • 35