Formatting a floating point does rounding?

Hi,

I was really surprised. My intention was to format the distance to precision two. Furthermore I want to draw a small circle for each 100 m up to 500 m. I did following to format the distance:
distanceField = dist.format("%.2f");

To get the number of circles I used:
numberOfCircles = (dist*100).toNumber() % 5;

By doing this I realized that the small drew circles appeared at a different time than the distanceField increased the 100 m. I digged into that problem and realized that the format command does not only a formating of the float but also does a rounding and that's the problem. E. g. a distance of 0.03645 is rounded to 0.04.

I have this formatting in all my data fields which means that the datafields display a (small) inaccuracy. Is there a way to change the format behaviour or do I have to do a
dist += 0.005;
before I use the format command?

Update: The correction value has to be
dist -= 0.004999
  • It's rounding to two decimal places because that's what you asked for by using "%.2f". My understanding is that Monkey C is supposed to mimic the C printf standard for these printf-style formatters, and that's how it works in C.

    https://developer.garmin.com/downloa...nstance_method
    The formatting string is similar to that avilable in printf from the C stdio library, though the length option is not available:


    If you want to truncate distanceField to 2 decimal places in the exact same way that you truncate the intermediate result in numberOfCircles, why not just use the same code in both places:

    var distTruncated = (dist*100).toNumber().toFloat()/100; // truncate to 2 decimal places
    distanceField = distTruncated.format("%.2f");


    That way you don't have to worry about trying to replicate the behaviour of toNumber() (which truncates or rounds towards 0), with a correction factor.