-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatMoney.js
89 lines (66 loc) · 3.14 KB
/
formatMoney.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
document.addEventListener("DOMContentLoaded", function() {
/**
* FORMAT MONEY
*
* @param {string|integer} rawNumber Number to be formatted.
* @param {integer} fractionDigits how many digits will be in the decimal section
* @param {string} decimalSign sign of the separator of decimal section
* @param {string} thousandsSeparator sign of the separator of thousands section
*
* @return {string} formatted number
*/
function formatMoney(rawNumber, fractionDigits, decimalSign, thousandsSeparator) {
// If the number parameter is omitted, it returns an error message and returns false.
if ( typeof (rawNumber) === "undefined" ) {
console.error("formatToMoney Function -> A number must be given as the first parameter of the formatToMoney function.");
return false;
}
// decimalSign and thousandsSeparator cannot be the same value.
if(decimalSign == thousandsSeparator) {
console.error("formatToMoney Function -> DecimalSign and ThousandsSeparator cannot be the same value.");
return false;
}
// declare variables.
var intPartStr, decPartStr, intPartNum, decPartNum, rawNumberStr, neg, firstSirstSeparatorIndex;
// default value of fraction digits = 2
fractionDigits = isNaN(fractionDigits = Math.abs(fractionDigits)) ? 2 : fractionDigits;
// default value of decimal sign = ","
decimalSign = typeof (decimalSign) === "undefined" ? "," : decimalSign;
// default value of thousands separator = "."
thousandsSeparator = typeof (thousandsSeparator) === "undefined" ? "." : thousandsSeparator;
// maybe another type
rawNumberStr = rawNumber.toString();
// purify the number from thousandsSeparators. RegExp much slower than string search.
if(rawNumberStr.search(thousandsSeparator) != -1) {
rawNumberStr = rawNumberStr.split(thousandsSeparator).join("");
}
// take digit and fraction of the number
if(rawNumberStr.search(decimalSign) != -1) {
if (rawNumberStr.substr(-1) == decimalSign) {
intPartStr = rawNumberStr;
decPartStr = "0".repeat(fractionDigits);
} else {
rawNumberStr_splitted = rawNumberStr.split(decimalSign);
intPartStr = rawNumberStr_splitted[0];
decPartStr = rawNumberStr_splitted[1];
}
} else {
intPartStr = rawNumberStr;
decPartStr = "0".repeat(fractionDigits);
}
intPartNum = parseInt(intPartStr);
// number -> positive or negative.
neg = intPartNum < 0 ? "-" : "";
intPartNum = Math.abs(intPartNum);
intPartStr = intPartNum.toString();
// an interesting rounding attempt :)
decPartStr = ( (decPartStr.length > fractionDigits) && (parseInt( decPartStr.substr(fractionDigits, 1) ) >= 5) ) ? decPartStr.substr(0, fractionDigits - 1) + (parseInt( decPartStr.substr(fractionDigits - 1, 1) ) + 1).toString() : decPartStr;
// Thousands Separator first index.
firstSeparatorIndex = (firstSeparatorIndex = intPartStr.length) > 3 ? firstSeparatorIndex % 3 : 0;
// RETURN
return neg +
(firstSeparatorIndex ? intPartStr.substr(0, firstSeparatorIndex) + thousandsSeparator : "") +
intPartStr.substr(firstSeparatorIndex).replace(/(\d{3})(?=\d)/g, "$1" + thousandsSeparator) +
( (fractionDigits) ? decimalSign + decPartStr.substr(0, fractionDigits) : "");
}
});