如果 javascript 中前 6 位数字为零,我需要解决错误。 我尝试使用此代码,但它不起作用
const value = 0000001234000444;
const str = value.toString();
if(str.substring(0,6) == "000000"){
console.log('value is start with zero')
}
最佳答案
JavaScript(以及许多其他语言)中的数字中省略了前导零。
您可以选择使用字符串:
const value = '0000001234000444';
if (value.indexOf('000000') === 0)
console.log('value starts with zero');
或者使用String.prototype.padStart
如果您知道所需的值的确切长度:
const value = 0000001234000444;
const str = value.toString().padStart(16,'0');
if (value.indexOf('000000') === 0)
console.log('value starts with zero');
关于javascript - 如何在javascript中找到我的前六个文本输入值为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56683224/