Results 1 to 8 of 8

Thread: Currency datatype functions

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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;
    }

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Currency datatype functions

    These functions are used like this:

    Code:
    // Medical must always be filled first
    item.Medical = parseCurrency(((parseMoney(item.DedAmt) + parseMoney(item.Premium))
         - (parseMoney(item.Dental) + parseMoney(item.Life))) + parseMoney(item.ApplyCredit));
    
    item.OverUnder = parseCurrency(parseMoney(item.Medical) - parseMoney(item.TotalCost));
    if ((parseMoney(item.OverUnder) >= -5) && (parseMoney(item.OverUnder) <= 5)) {
         item.OverUnder =  parseCurrency(0);
    }
    item.Balance = parseCurrency(parseMoney(item.OverUnder) - parseMoney(item.ToCredit)
          - parseMoney(item.AdjAmt));
    item.AcctCredit = parseCurrency((parseMoney(item.LMAcctCredit)
          - parseMoney(item.ApplyCredit)) + parseMoney(item.ToCredit))

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Currency datatype functions

    http://jsfiddle.net/mn9jpfgt/

    I obviously don't know how to make a fiddle...

    Any help would be greatly appreciated!

    I wanted to just show a couple of ALERT calls to show the functionality in action. What would I put in the HTML window to make that happen?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: Currency datatype functions

    Visual Basic 6's Currency data type is specifically designed to hold data that represents money amounts. Discover several advantages to using the Currency data type.

    Many VB6 programmers automatically use the Single data type for holding data that represents money amounts. You should be aware that the Currency data type is, as the name implies, specially designed for this purpose and offers some advantages.

    Currency is a fixed-point data type, as opposed to the floating-point Single. In other words, it is always accurate to a specific number of decimal places, four in this case. While the Single type can represent many more decimal places, these are not needed in Currency calculations and, in fact, can introduce rounding errors. These errors are small but can have an effect on overall accuracy.

    The Currency data type is actually an integer type internally. In use, it is scaled by a factor of 10,000 to give four digits to the right of the decimal point. It permits up to 15 digits to the left of the decimal point, resulting in a range of approximately -922,337,000,000,000 to +922,337,000,000,000. An individual type Currency variable takes up 8 bytes of memory, compared to 4 bytes for Single

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,854

    Re: Currency datatype functions

    This thread is about using a currency in Javascript, it has nothing to do with VB6 itself.

  6. #6
    Fanatic Member Peter Porter's Avatar
    Join Date
    Jul 2013
    Posts
    532

    Re: Currency datatype functions

    D'oh!
    Last edited by Peter Porter; May 18th, 2020 at 11:43 AM.

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Currency datatype functions

    I even mention VB6 and the Currency datatype in my top post! I kind of mimicked that behavior, lol!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,207

    Re: Currency datatype functions

    Quote Originally Posted by szlamany View Post
    I obviously don't know how to make a fiddle...
    ...
    I wanted to just show a couple of ALERT calls to show the functionality...
    Newest jsfiddles now support console.log(...).

    http://jsfiddle.net/L7wsuv5g/

    The above link demonstrates the functionality of your functions
    (though I've renamed them to my taste and shortened them quite a bit)...

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width