## Currency datatype functions

These two functions allow you to display strings of "currency" formatted values and also use those strings in math functions. The strings are converted to integer datatype with an implied decimal point (just like CURRENCY in VB6).

First function - parseMoney - takes a STRING argument and returns an integer value.

Code:
```function parseMoney(strMoney) {
var intMoney = parseInt(0, 10);
var intDecimal = 0;
var intLength = 0;
if (!(typeof strMoney == "string")) {
strMoney = String(strMoney);
}
if (strMoney.length != 0) {
strMoney = strMoney.replace(/,/g, "").replace("\$", "").replace("**","");
intDecimal = strMoney.indexOf(".");
intLength = strMoney.length;
if (intDecimal == -1) {
strMoney = strMoney + ".00";
} else {
if ((intDecimal + 3) != intLength) {
if (intLength == (intDecimal + 1)) {
strMoney = strMoney + "00";
} else if (intLength == (intDecimal + 2)) {
strMoney = strMoney + "0";
} else {
strMoney = strMoney.substring(0, intDecimal + 3);
}
}
}
intMoney = parseInt(strMoney.replace(".", ""), 10);
}
return intMoney;
}```
Second function - parseCurrency - will take that integer "currency" value as the argument and return a formatted string.

Code:
```function parseCurrency(intMoney) {
var strMoney = "";
var blnIsNeg = false;
strMoney = String(intMoney);
if (strMoney.substring(0, 1) == "-") {
blnIsNeg = true;
strMoney = strMoney.substring(1, strMoney.length);
}
var intLength = strMoney.length;
if (intLength < 3) {
strMoney = ("000" + strMoney).substring(intLength, intLength + 3);
intLength = 3;
}
strMoney = ((blnIsNeg) ? "-" : "") + addCommas(strMoney.substring(0, intLength - 2)) + "." + strMoney.substring(intLength - 2);
return strMoney;
}```
This function - addCommas - is needed for support of the parseCurrency function.

Code:
```function addCommas(sValue) {
var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');

while (sRegExp.test(sValue)) {
sValue = sValue.replace(sRegExp, '\$1,\$2');
}
return sValue;
}```