parseInt cannot parse '08' and '09' but all other strings.
-
Now this is a interesting bug!
This code:
Console.print(parseInt('01')); Console.print(parseInt('02')); Console.print(parseInt('03')); Console.print(parseInt('04')); Console.print(parseInt('05')); Console.print(parseInt('06')); Console.print(parseInt('07')); Console.print(parseInt('08')); Console.print(parseInt('09')); Console.print(parseInt('10')); Console.print(parseInt('11')); Console.print(parseInt('12'));
produces this output:
Interface: 1 Interface: 2 Interface: 3 Interface: 4 Interface: 5 Interface: 6 Interface: 7 Interface: 0 Interface: 0 Interface: 10 Interface: 11 Interface: 12
@Christoph-Hart any idea why this might be the case? Why the 08 and 09 is there some deeper meaning to this?
-
@oskarsh Yeah this is a known thing,
parseInt()
decides if the string contains a int or a hex and is fooled by the 08...
I prefer to useMath.round()
instead.
I just discoveredString.getIntValue()
but I haven't yet tried and don't know if there's a perf difference, trying.... -
@ustk Interesting! Thanks for the insights. String.getIntValue() works as expected.
-
@oskarsh Made some test, better to stick with the the good ol' math function...
const var test = ["321", "1564.21", "text", "08"]; reg tmp; Console.startBenchmark(); for (t in test) { for (i=0;i<100000;i++) //tmp = t.getIntValue(); // 174ms tmp = Math.round(t); // 100ms } Console.stopBenchmark();
-
@ustk yeah it has to resolve two things (the object t and the function getIntValue) vs. one (only the object, the math function is resolved at compile time.
But this performance difference is neglible - in UI contexts there are much slower things and if you are using anything string related in an audio processing context you have to sit back and evaluate life decisions that lead you into this situation of misery anyway.
Also Math.round still returns a float number IIRC while getIntValue makes sure that the var instance uses the integer data slot internally.