SoFunction
Updated on 2025-03-09

Various ways to judge an integer in JavaScript and methods to retain two decimals

1. Use the remaining operator to judge

Any integer will be divisible by 1, that is, the remainder is 0. Use this rule to determine whether it is an integer.

function isInteger(obj) {
 return obj%1 === 0
}
isInteger(3) // true
isInteger(3.3) // false 

The above output shows that this function is very useful, but it seems to be incompetent for strings and some special values.

isInteger('') // true
isInteger('3') // true
isInteger(true) // true
isInteger([]) // true

It is really hard to accept that empty strings, string type numbers, boolean true, and empty arrays all return true. For details of these types of internal conversion, please refer to:

Weird fake value in JavaScript

Therefore, it is necessary toDetermine whether the object is a number, for example, add a typeof

function isInteger(obj) {
 return typeof obj === 'number' && obj%1 === 0
}
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

Well, this is more perfect.

2. Use, judge

The integer is still equal to yourself. Use this feature to determine whether it is an integer. Examples are as follows

function isInteger(obj) {
 return (obj) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

This directly blocks the string, true, [], and the amount of code is smaller than the previous function.

3. Judgment through parseInt

function isInteger(obj) {
 return parseInt(obj, 10) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

Very good, but also has one disadvantage

isInteger(1000000000000000000000) // false

It actually returned false, it was unreasonable. The reason is that parseInt forces the first parameter to be parsed into a string before parsing the integer. This method of converting numbers into integers is not a good choice.

4. Judgment through bit operations

function isInteger(obj) {
 return (obj | 0) === obj
}
isInteger(3) // true
isInteger(3.3) // false
isInteger('') // false
isInteger('3') // false
isInteger(true) // false
isInteger([]) // false

This function is very good and has high efficiency. But there is a flaw. As mentioned above, bit operations can only process numbers within 32 bits, and they are powerless to do anything more than 32 bits, such as

isInteger((2, 32)) // digits above 32 digits have returned false

Of course, most of the time we don’t use such a large number.

5. ES6 provides

(3) // true
(3.1) // false
('') // false
('3') // false
(true) // false
([]) // false

Currently, the latest Firefox and Chrome are already supported.

6. After entering the int type number, automatically add .00 to the next

var getFloatStr = function (num) {
 num += '';
 num = (/[^0-9|\.]/g, ''); //Clear non-number non-character in string if (/^0+/) //Clear 0 at the beginning of the string  num = (/^0+/, '');
 if (!/\./.test(num)) //Add .00 at the end of the entire string of numbers  num += '.00';
 if (/^\./.test(num)) // When the character starts with ., add 0 at the beginning  num = '0' + num;
 num += '00'; //Fill zero at the end of the string num = (/\d+\.\d{2}/)[0];
 return num;
 };

If vue is used globally

 = function(num) {
 num += '';
 num = (/[^0-9|\.]/g, ''); //Clear non-number non-character in string
 if (/^0+/) //Clear 0 at the beginning of the string num = (/^0+/, '');
 if (!/\./.test(num)) //Add .00 at the end of the entire string of numbers num += '.00';
 if (/^\./.test(num)) // When the character starts with ., add 0 at the beginning num = '0' + num;
 num += '00'; //Fill zero at the end of the string num = (/\d+\.\d{2}/)[0];
 return num;
}

There are many ways, for example:

js converts decimal to retain two decimal places (retains 0.00 and not retains)

function toDecimal2(x) {
 var f = (x * 100) / 100;
 var s = ();
 var rs = ('.');
 if (rs < 0) {
 rs = ;
 s += '.';
 }
 while ( <= rs + 2) {
 s += '0';
 }
 return s;
}
//Retain 2 decimal places, such as: 2, 2 will be retained and 0 will not be addedfunction toDecimal2NoZero(x) {
 var f = (x * 100) / 100;
 var s = ();
 return s;
}

Summarize

The above is the various ways to judge an integer in JavaScript and retain two decimal places in JavaScript. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website! If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!