Hướng dẫn javascript check if string is a number - javascript kiểm tra xem chuỗi có phải là số không

TL;DR

Nó phụ thuộc phần lớn vào những gì bạn muốn phân tích cú pháp như một số.

So sánh giữa các chức năng tích hợp

Vì không có nguồn nào hiện tại thỏa mãn tâm hồn tôi, tôi đã cố gắng tìm ra những gì thực sự xảy ra với các chức năng này.

Ba câu trả lời ngay lập tức cho câu hỏi này cảm thấy như:

  1. !isNaN(input) (cung cấp đầu ra giống như +input === +input)
  2. !isNaN(parseFloat(input))
  3. isFinite(input)

Nhưng có ai trong số họ đúng trong mọi kịch bản không?

Tôi đã kiểm tra các chức năng này trong một số trường hợp và tạo ra đầu ra là Markdown. Đây là những gì nó trông giống như:

input!isNaN(input) hoặc +input===+input
+input===+input
!isNaN(
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
0
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
1
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
0
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
1
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
2
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
3
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
3
Bình luận
123 -
'123'-
12.3 -
'123'-
'123'-
'123'-
'123''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.-
'123'-
'123'-
'123''12 .3 '-
'123''12 .3 '-
'123''12 .3 ''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.-
But caution! It's way more larger than the maximum safe integer value 253 (about 9×1015). Read this for details.
'123''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Though not as crazy as it may seem. In JavaScript, a value larger than ~10308 is rounded to infinity, that's why. Look here for details.
And yes,
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
5 considers infinity as a number, and
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
4 parses infinity as infinity.
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 '-
Then why
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
7 should return a
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
8 here? Someone please explain this design concept to me.
'123''12 .3 ''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '

Khoảng trắng rỗng được cắt tỉa, như mong đợi.

1_000_000it depends largely on what we need. For example, we may want to consider a null input as 0. In that case !isNaN(input)1 will work fine.

Phân tách số hiểu, cũng được mong đợi.

'1_000_000'

/**
 * My necessity was met by the following code.
 */

if (input === null) {
    // Null input
} else if (input.trim() === '') {
    // Empty or whitespace-only string
} else if (isFinite(input)) {
    // Input is a number
} else {
    // Not a number
}

Bất ngờ! JS sẽ không phân tách phân tách số bên trong một chuỗi. Để biết chi tiết, kiểm tra vấn đề này. (Tại sao sau đó phân tích cú pháp khi phao hoạt động mặc dù? Chà, nó không. 😉)

/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);

Làm thế nào để bạn kiểm tra xem một chuỗi có một số trong javascript?

Để kiểm tra xem một chuỗi có chứa các số trong javascript, hãy gọi phương thức test () trên regex này: / \ d /. test () sẽ trả về true nếu chuỗi chứa số. Nếu không, nó sẽ trả về sai. Phương thức kiểm tra regexp () tìm kiếm sự phù hợp giữa biểu thức chính quy và chuỗi.call the test() method on this regex: /\d/ . test() will return true if the string contains numbers. Otherwise, it will return false . The RegExp test() method searches for a match between a regular expression and a string.

Làm thế nào để bạn kiểm tra xem một chuỗi là một số?

Cách dễ nhất để kiểm tra xem một chuỗi có phải là số hay không là bằng cách sử dụng một trong các phương thức Java tích hợp sau:..
Số nguyên. ParseInt ().
Số nguyên. giá trị của().
Kép. Parsedouble ().
Trôi nổi. parsefloat ().
Dài. Parselong ().

Làm thế nào để bạn kiểm tra xem một giá trị là một số trong javascript?

Bạn có thể kiểm tra xem một giá trị có phải là một số theo ba cách:..
Loại - nếu giá trị là một số, chuỗi "số" được trả về ..
Con số.isfinite () - Nếu giá trị là một số, true được trả về ..
isnan () - Nếu giá trị là một số, sai được trả về ..

Làm thế nào để bạn kiểm tra xem một số chuỗi có phải là số nguyên không?

Cách hiệu quả nhất để kiểm tra xem một chuỗi là số nguyên trong Python là sử dụng phương thức str.isDigit (), vì cần ít thời gian nhất để thực thi.STR.Phương thức isDigit () trả về true nếu chuỗi đại diện cho một số nguyên, nếu không thì sai.use the str. isdigit() method, as it takes the least time to execute. The str. isdigit() method returns True if the string represents an integer, otherwise False .

Làm thế nào để bạn kiểm tra xem một chuỗi là một số trong TypeScript?

Để kiểm tra xem một chuỗi là số hợp lệ trong TypeScript:..
Kiểm tra xem chuỗi không phải là một chuỗi trống hay chỉ chứa khoảng trắng ..
Chuyển chuỗi vào số.phương pháp isnan () ..
Nếu số.isnan trả về false, chuỗi là một số hợp lệ ..