2

On this page, https://emperorbob7.github.io/JSheets/, I have a function called TYPE, syntax for it is linked on the page, the RegEx used for the decimal detection function is located in codeblock below*

Once I put in too many numbers however, the TYPE says the cell contains a decimal despite none being there. Is this an automatic function that adds a . whenever a number exceeds a certain limit?

Example case: 3123123123123123123122312312312 Output: Decimal Edit:

function TYPE() {
 const regex = /\.[0-9]/;
 if(arguments[0] == "true" || arguments[0] == "false")
  return "Boolean";
 if(isNaN(arguments[0]))
  return "String";
 else if(regex.test(arguments[0]))
  return "Decimal";
 else
  return "Integer";
}

Code^ Sorry for not posting it before, will keep it in mind for the future.

Sorry for the badly worded question, thanks in advance

  • 3
    `.` means "any character" in regex. Did you mean `\.`? – ggorlen Jan 24 '20 at 18:44
  • 1
    you need to escape the dot as @ggorlen says – Sebastián Espinosa Jan 24 '20 at 18:44
  • Does this answer your question? [Regular expression to match a dot](https://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot) – ggorlen Jan 24 '20 at 18:46
  • 2
    Please add code samples to your question, not just a link to the page containing the javascript function you're talking about. It's asking a lot of people here who might try to help your when you require them to do a _lot_ of work just to see your code. – Nicholas Carey Jan 24 '20 at 19:13
  • The Post somehow removed the backslash from my regex :/ The actual regex is this: /\.[0-9]/ so yes I did have the \. but it appears someone else answered it already, I will edit the main post, thank you for the quick response though – Emperor Bob Jan 24 '20 at 19:47

2 Answers2

3

You have an integer that is larger than the Number object can handle (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER), so when it's converted to a string for the regex it becomes an exponential value.

Try console.log(3123123123123123123122312312312); and you will get 3.123123123123123e+30

Or

let val = 3123123123123123123122312312312;
val.toString();

"3.123123123123123e+30"

You can also test your value with Number.isSafeInteger(3123123123123123123122312312312);, which returns false.

The solution is to use Number.isInteger(); as your test instead of a regex. It correctly returns true for your large number.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger

Ed Lucas
  • 5,955
  • 4
  • 30
  • 42
  • Thank you for the `Number.isInteger()` note, it will be helpful, thank you lucas – Emperor Bob Jan 24 '20 at 19:53
  • You had the right idea, but `3123123123123123123122312312312` is actually within the range of `Number.MAX_VALUE` (`3.123123123123123e+30` is less than `1.7976931348623157e+308`). All numbers larger than that would convert to the value `Infinity`. – chris Jan 24 '20 at 20:07
  • Yep. I just dropped in the wrong link. I'll change it to point to MAX_SAFE_INTEGER. – Ed Lucas Jan 24 '20 at 20:18
1

Javascript can only store integers up to 9007199254740991 safely. Beyond that, javascript may convert the number to power notation (i.e., 5000000000000000000000 becomes 5e+21) or start converting digits into zeros for storage.

var n = 3123123123123123123122312312312;
console.log(n);
/* Output: 3.123123123123123e+30 */

You can use Number.isSafeInteger() to test whether the number is within the safe range, and then apply your original code to it in that case. If not, you can perform a different test (such as a test against /\d\.\d+e\+\d+ ) to see whether the decimal included is due to exponent notation.

Also be aware that a number in exponent notation will test true using Number.isInteger(), even if it was a floating point to begin with, as that information will be lost.

var x_int = 3123123123123123123122312312312;
var x_flt = 3123123123123123123122312312312.333333333;
console.log( x_int === x_flt);
/* Output: true */

console.log(Number.isInteger(x_flt));
/* Output: true */
chris
  • 789
  • 4
  • 16