Add more precision options to to_human().

This commit is contained in:
Stefan Thomas
2012-12-16 11:50:04 -08:00
parent 96c93247e6
commit 2baccc4800

View File

@@ -1156,6 +1156,9 @@ Amount.prototype.to_text = function (allow_nan) {
* *
* @param opts Options for formatter. * @param opts Options for formatter.
* @param opts.precision {Number} Max. number of digits after decimal point. * @param opts.precision {Number} Max. number of digits after decimal point.
* @param opts.min_precision {Number} Min. number of digits after dec. point.
* @param opts.skip_empty_fraction {Boolean} Don't show fraction if it is zero,
* even if min_precision is set.
* @param opts.group_sep {Boolean|String} Whether to show a separator every n * @param opts.group_sep {Boolean|String} Whether to show a separator every n
* digits, if a string, that value will be used as the separator. Default: "," * digits, if a string, that value will be used as the separator. Default: ","
* @param opts.group_width {Number} How many numbers will be grouped together, * @param opts.group_width {Number} How many numbers will be grouped together,
@@ -1187,10 +1190,18 @@ Amount.prototype.to_human = function (opts)
int_part = int_part.replace(/^0*/, ''); int_part = int_part.replace(/^0*/, '');
fraction_part = fraction_part.replace(/0*$/, ''); fraction_part = fraction_part.replace(/0*$/, '');
if (fraction_part.length || !opts.skip_empty_fraction) {
if ("number" === typeof opts.precision) { if ("number" === typeof opts.precision) {
fraction_part = fraction_part.slice(0, opts.precision); fraction_part = fraction_part.slice(0, opts.precision);
} }
if ("number" === typeof opts.min_precision) {
while (fraction_part.length < opts.min_precision) {
fraction_part += "0";
}
}
}
if (opts.group_sep) { if (opts.group_sep) {
if ("string" !== typeof opts.group_sep) { if ("string" !== typeof opts.group_sep) {
opts.group_sep = ','; opts.group_sep = ',';