Hallo Der Martin,
also die Anzahl der Vorkommastellen? Dann ist der dekadische Logarithmus (also der Logarithmus zur Basis 10) das Mittel der Wahl. Der ist nämlich immer um 1 weniger als die Stellenzahl, solange das Argument positiv und größer oder gleich 1 ist.
Die Formulierung stimmt so nicht.
Es wird impliziert, dass der Logarithmus immer eine natürliche Zahl ist. Du meinst den ganzzahligen Anteil des Logarithmus.
parseInt(Math.log10(Math.abs(x))) + 1
sollte für alle Varianten das richtige Ergebnis liefern:
console.log(parseInt(Math.log10(Math.abs( 5.5))) + 1) // 1
console.log(parseInt(Math.log10(Math.abs( 0.5))) + 1) // 1
console.log(parseInt(Math.log10(Math.abs( -0.5))) + 1) // 1
console.log(parseInt(Math.log10(Math.abs( -5.5))) + 1) // 1
console.log(parseInt(Math.log10(Math.abs(-15.5))) + 1) // 2
Bis demnächst
Matthias