JavaScript Number 保留前导 0

2022-01-17 00:00:00 numbers javascript

我有一个问题,我为邮政编码构建了一个非常简单的 JavaScript 搜索.我使用 JS Numbers 是因为我想检查传递的数字(搜索词)是否小于||等于或大于||等于最大值和最小值.

I have a problem, I build a very simple javascript search for postal codes. I am using JS Numbers because I want to check if the passed number (search term) is less||equal or more||equal to the max and min.

value >= splitZips[0] && value <= splitZips[1]

但是 Javascript Number var 类型删除了前导 0,这是一个问题,因为我有 01075 之类的邮政编码以及 8430 之类的邮政编码.所以它找不到小 4 位代码.

But the Javascript Number var type deletes leading 0, which is a problem because I have postal codes like 01075 and also postal codes like 8430. So it can not find the small 4 digit codes.

知道如何解决这个问题吗?

Any idea how to fix this?

推荐答案

将它们表示为 String.在严格模式之外,前导零表示否则为八进制数.

Represent them as a String. Outside of strict mode, a leading zero denotes an octal number otherwise.

另外,为什么在计算数字时前导零有任何意义?如果需要,只需使用 parseInt(num, 10).

Also, why would a leading zero have any significance when calculating numbers? Just use parseInt(num, 10) if you need to.

相关文章