Removing trailing 0s

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);