Lose the .0 in label / value
-
-
-
@DanH you don't need to create a variable for this as it is simple to understand, you can directly put the round function inside the label set
-
@DanH said in Lose the .0 in label / value:
even if the value is an integer, eg 1.0. 2.0
Those aren't integers ;)
You could also use parseInt()
-
@d-healey thanks, should have used a full stop rather than a comma
-
@ustk said in Lose the .0 in label / value:
@DanH @Matt_SF doubleToString parameters are the other way round
Engine.doubleToString(double value, int digits)
In this case a simpleMath.round(value)
will remove the last digit the other function can'tMy mistake, sorry
-
@d-healey said in Lose the .0 in label / value:
You could also use parseInt()
I don't like the parseInt function, because, in some (rare) cases, it treats a leading zero as an octal number. I agree this is rarely the case (it happens for instance when dealing with dates).
example:
Console.print(parseInt("0500")); // => 320 Console.print(Math.round("0500")); // => 500
Though in this case, it is not a problem to use it, I just personally feel like wanting to stay away from it :)
EDIT:
I just discovered that when not using a string, both are converting octal to base10!
(this makes sense since they are written in an octal fashion...)Console.print(parseInt(0500)); // => 320 Console.print(Math.round(0500)); // => 320
But I don't see a scenario where this could happen anyway...
-
@ustk Is there an efficiency difference between the two functions?
-
@d-healey That is what I'm wondering... Making a benchmark to see...
-
@d-healey Another reason to stick with
Math.round()
!reg a = 0; inline function round(dec) { return Math.round(dec); } Console.startBenchmark(); for (i=0;i<100000;i+=0.3333) { a = parseInt(i); // 130-135ms a = Math.round(i); // 50-53ms a = round(i); // 68-70ms } Console.stopBenchmark();
-
@ustk Good test, I get similar results on my system. So
Math.round()
is the way to go!