Hello,
I'm trying to format a number to 3 d.p. without trailing zeros, which
would appear to require a pattern like:
###,###.###
If you format an integer using this pattern, you obtain a result with
the suffix '.000', the same as if you had specified trailing zeros in
the pattern. I would like to only see the zeros if the number has been
rounded down, to indicate that there are more digits which are not
visible.
Is there an obvious reason why the formatter behaves in this manner?
Below is a patch to trim redundant trailing zeros before the required
trailing zeros are added. I accept that this would break existing
behaviour, so may not be of use to anyone else.
Thanks,
Mike.
Index: Format.js
===================================================================
--- Format.js (revision 1269)
+++ Format.js (working copy)
@@ -48,6 +48,7 @@
num = num * 100.0;
curfooter = fmt.percent + curfooter;
}
+ unroundedNum = num;
num = MochiKit.Format.roundToFixed(num, precision);
var parts = num.split(/\./);
var whole = parts[0];
@@ -66,10 +67,13 @@
}
res = whole + res;
if (precision > 0) {
+ if (num == unroundedNum)
+ frac = frac.replace(/0+$/, '');
while (frac.length < trailingZeros) {
frac = frac + "0";
}
- res = res + fmt.decimal + frac;
+ if (frac.length > 0)
+ res = res + fmt.decimal + frac;
}
return curheader + res + curfooter;
};
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---