Friday, March 11, 2011

Formatting Currency in Microsoft Dynamics CRM 2011 or 4.0 in Jscript

Hello everyone, I recently had a really hard time trying to find a good currency formatting function for Jscript that worked with CRM 2011 Jscript standards.  I found a few functions that I could use together to do it, I have now just rolled them up into one for you guys. 

//Format currency in CRM jscript

//EXAMPLE USAGE
//alert(FormatAsCurrency(10000));

function FormatAsCurrency(amount) {

    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    amount = s;
    var delimiter = ","; // replace comma if other mark is desired
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;

    return "$" + amount;

}

I hope this helps, and Happy Friday!!!

No comments:

Post a Comment