Mod and Format Currency Numbers

[Total: 0    Average: 0/5]

This code can be used “as it is” to convert a Numeric value to a human readable string. Remember, after conversion, the value is a string and not usable for math values. To use the number for math – call the original number and convert it for dispaly aft

Body Code:

<script language="JavaScript">
<!--
/*
 * convert to two digits and currency format
 */
function conDec(tmp)
{
    var alertbox=false;
    var tmp;
    tmp = tmp*100;
    tmp = Math.round(tmp);
    if(isNaN(tmp)) {tmp = 0.00;}
    if(tmp>99999999999)alert("trillion is not supported - only millions.");
    /* converts tmp to decimal cents and adds a 0 to .1 to .9 or adds .00 to 0 */
    cen = tmp%100;
    if(cen < 0){cen = ".00"}
    else if(cen <10) {cen = ".0"+cen}
    else if(cen < 100){cen = "."+cen}
    else {cen = cen}
    if(alertbox)alert("Cents "+cen);  /* Cents alert dialog box */

    tmp = parseInt(tmp/100);
    hun = tmp - ((parseInt(tmp/1000)) *1000 );
    if(hun > 0 && hun <1000) {hun = hun}
    else {hun = ''}
    if(alertbox)alert("Hundreds "+hun); /* Hundreds alert dialog box */

    tmp = parseInt(tmp/1000)
    tho = tmp -((parseInt(tmp/1000)) *1000);
    if(tho > 0 && tho < 1000) {tho = tho +","}
    else {tho = ''}
    if(alertbox)alert("Thousands "+tho);  /* Thousands dialog box */


    tmp = parseInt(tmp/1000);
    mil = tmp -((parseInt(tmp/1000)) *1000);
    if(mil > 0 && mil < 1000) {mil = mil + ","}
    else {mil = ''}
    if(alertbox)alert("Millions "+mil); /* Millions dialog box */
    if(alertbox)alert("$"+mil+tho+hun+cen); /* Convert tmp to Currency */




    if(document.getElementById)
     document.getElementById("result").innerHTML="$"+mil+tho+hun+cen;
    else
     alert("Your browser does not support getElementById.nAnswer=$"+mil+tho+hun+cen);
}

// -->
</script>
<form name="currencyFormat">
<per>
    Instructions:
        <ol>
            <li>Click the mouse in the text box.</li>
            <li>Enter a decimal or integer.</li>
            <li>Click anywhere outside the text box.</li>
        </ol>
        <br><br>
    <input type="reset" value="clear">
    <input type="text" value="" tabindex="1" onblur="conDec(this.value);">
    </form>
<br>
<table border=1><tr><td>
<span id=result>Answer Will Go Here</span>
</td></tr></table>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *