function countDigitsString(value) {
// Convert the number to a string
const text = String(value);
// If negative, ignore the leading '-'
if (text.startsWith("-")) {
return text.length - 1;
}
return text.length;
}
const number1 = -12345;
const digits1 = countDigitsString(number1);
console.log("Number:", number1);
console.log("Digit count (String method):", digits1);
/*
run:
Number: -12345
Digit count (String method): 5
*/