What about something like this:
var d = (discharge * 100).toNumber();
var f = (d%100 == 0 ? "%d" : (d%10 == 0 ? "%0.1f" : "%0.2f"));
return (d/100.0).format(f);
What about something like this:
var d = (discharge * 100).toNumber();
var f = (d%100 == 0 ? "%d" : (d%10 == 0 ? "%0.1f" : "%0.2f"));
return (d/100.0).format(f);
See https://forums.garmin.com/developer/connect-iq/f/discussion/419302/best-way-to-trim-trailing-zeros/1963008#1963008 for a generic solution - based on this one - that isn't hardcoded to a precision of 2 decimal places.
EDIT: "%0.xf" rounds, and we're both applying it after counting the number of decimals, which means that it can be tricky to prove that it isn't rounding in such a way that invalidates our count. (Yes, your solution seems to work on its face, but it's hard for me to *prove* that it works in all cases.)
Here's an even better answer that doesn't rely on the behaviour of "%0.xf" and it doesn't do any rounding after counting decimals.
This new answer rounds the value before determining the number of decimals (without trailing zeroes), then it manually converts the value to a string with the appropriate number of decimals, without rounding: