2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 04:17:36 +00:00
books/utils/numberFormat.js

148 lines
4.1 KiB
JavaScript
Raw Normal View History

const frappe = require('frappejs');
const { DEFAULT_DISPLAY_PRECISION, DEFAULT_LOCALE } = require('./consts');
2018-07-14 16:12:34 +00:00
const numberFormats = {
2019-08-28 08:54:11 +00:00
'#,###.##': { fractionSep: '.', groupSep: ',', precision: 2 },
'#.###,##': { fractionSep: ',', groupSep: '.', precision: 2 },
'# ###.##': { fractionSep: '.', groupSep: ' ', precision: 2 },
'# ###,##': { fractionSep: ',', groupSep: ' ', precision: 2 },
"#'###.##": { fractionSep: '.', groupSep: "'", precision: 2 },
'#, ###.##': { fractionSep: '.', groupSep: ', ', precision: 2 },
'#,##,###.##': { fractionSep: '.', groupSep: ',', precision: 2 },
'#,###.###': { fractionSep: '.', groupSep: ',', precision: 3 },
'#.###': { fractionSep: '', groupSep: '.', precision: 0 },
'#,###': { fractionSep: '', groupSep: ',', precision: 0 },
2019-08-28 08:54:11 +00:00
};
2018-07-14 16:12:34 +00:00
module.exports = {
// parse a formatted number string
// from "4,555,000.34" -> 4555000.34
2019-09-17 08:14:09 +00:00
parseNumber(number, format = '#,###.##') {
2018-07-14 16:12:34 +00:00
if (!number) {
return 0;
}
if (typeof number === 'number') {
return number;
}
const info = this.getFormatInfo(format);
return parseFloat(this.removeSeparator(number, info.groupSep));
},
2019-09-17 08:14:09 +00:00
formatNumber(number, format = '#,###.##', precision = null) {
2018-07-14 16:12:34 +00:00
if (!number) {
number = 0;
}
let info = this.getFormatInfo(format);
if (precision) {
info.precision = precision;
}
let is_negative = false;
number = this.parseNumber(number);
if (number < 0) {
is_negative = true;
}
number = Math.abs(number);
number = number.toFixed(info.precision);
var parts = number.split('.');
// get group position and parts
var group_position = info.groupSep ? 3 : 0;
if (group_position) {
var integer = parts[0];
var str = '';
for (var i = integer.length; i >= 0; i--) {
var l = this.removeSeparator(str, info.groupSep).length;
2019-08-28 08:54:11 +00:00
if (format == '#,##,###.##' && str.indexOf(',') != -1) {
// INR
2018-07-14 16:12:34 +00:00
group_position = 2;
l += 1;
}
str += integer.charAt(i);
if (l && !((l + 1) % group_position) && i != 0) {
str += info.groupSep;
}
}
parts[0] = str.split('').reverse().join('');
2018-07-14 16:12:34 +00:00
}
2019-08-28 08:54:11 +00:00
if (parts[0] + '' == '') {
parts[0] = '0';
2018-07-14 16:12:34 +00:00
}
// join decimal
2019-08-28 08:54:11 +00:00
parts[1] = parts[1] && info.fractionSep ? info.fractionSep + parts[1] : '';
2018-07-14 16:12:34 +00:00
// join
2019-09-17 08:14:09 +00:00
return (is_negative ? '-' : '') + parts[0] + parts[1];
2018-07-14 16:12:34 +00:00
},
getFormatInfo(format) {
let format_info = numberFormats[format];
if (!format_info) {
2019-10-08 11:16:12 +00:00
throw new Error(`Unknown number format "${format}"`);
2018-07-14 16:12:34 +00:00
}
return format_info;
},
round(num, precision) {
var is_negative = num < 0 ? true : false;
var d = parseInt(precision || 0);
var m = Math.pow(10, d);
var n = +(d ? Math.abs(num) * m : Math.abs(num)).toFixed(8); // Avoid rounding errors
2019-08-28 08:54:11 +00:00
var i = Math.floor(n),
f = n - i;
var r = !precision && f == 0.5 ? (i % 2 == 0 ? i : i + 1) : Math.round(n);
2018-07-14 16:12:34 +00:00
r = d ? r / m : r;
return is_negative ? -r : r;
},
removeSeparator(text, sep) {
2019-08-28 08:54:11 +00:00
return text.replace(new RegExp(sep === '.' ? '\\.' : sep, 'g'), '');
},
getDisplayPrecision() {
return frappe.SystemSettings.displayPrecision ?? DEFAULT_DISPLAY_PRECISION;
},
getCurrencyFormatter() {
if (frappe.currencyFormatter) {
return frappe.currencyFormatter;
}
const locale = frappe.SystemSettings.locale ?? DEFAULT_LOCALE;
const display = this.getDisplayPrecision();
return (frappe.currencyFormatter = Intl.NumberFormat(locale, {
style: 'decimal',
minimumFractionDigits: display,
}));
},
formatCurrency(value) {
const currencyFormatter = this.getCurrencyFormatter();
if (typeof value === 'number') {
return currencyFormatter.format(value);
}
if (value.round) {
const displayPrecision = this.getDisplayPrecision();
return currencyFormatter.format(value.round(displayPrecision));
}
const formattedCurrency = currencyFormatter(value);
if (formattedCurrency === 'NaN') {
throw Error(
`invalide value passed to formatCurrency: '${value}' of type ${typeof value}`
);
}
return formattedCurrency;
},
2018-07-14 16:12:34 +00:00
};