navigation  interaction  search

 other resources

Changeset 361

Show
Ignore:
Timestamp:
11/09/07 15:00:09
Author:
brieb
Message:

Fixed JavaScript? numberToCurrency function (it was not rounding properly)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phpbms/common/javascript/common.js

    r349 r361  
    673673 
    674674function numberToCurrency(number){ 
     675         
    675676        var currency=""; 
     677         
    676678        if(isNaN(parseFloat(number))) number=0; 
    677679 
    678680        if(number<0) 
    679681                currency+="-"; 
    680         number=Math.abs(number); 
    681         currency+=CURRENCY_SYM; 
     682 
     683        number = Math.abs(number); 
     684         
     685        currency += CURRENCY_SYM; 
     686         
    682687        if(number>0 && number <0) 
    683688                currency+="0"; 
    684689 
    685         var withThousands=parseInt(number).toString(); 
     690        var lessthanone = Math.round( (number-parseInt(number)) * (Math.pow(10,CURRENCY_ACCURACY)) ); 
     691         
     692        if(lessthanone >= Math.pow(10,CURRENCY_ACCURACY)){ 
     693 
     694                number++; 
     695                         
     696                lessthanone -= Math.pow(10,CURRENCY_ACCURACY); 
     697                 
     698        }//end if 
     699         
     700        lessthanone = lessthanone.toString() 
     701         
     702        while(lessthanone.length<CURRENCY_ACCURACY) 
     703                lessthanone = "0" + lessthanone;                 
     704 
     705        var withThousands = parseInt(number).toString(); 
    686706        var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})'); 
     707 
    687708        while(objRegExp.test(withThousands)) 
    688709       withThousands = withThousands.replace(objRegExp, '$1'+THOUSANDS_SEPARATOR+'$2'); 
    689  
    690         var lessthanone=Math.round((number-parseInt(number))*(Math.pow(10,CURRENCY_ACCURACY))).toString(); 
    691         while(lessthanone.length<CURRENCY_ACCURACY) 
    692                 lessthanone="0"+lessthanone; 
     710                 
    693711        currency+=withThousands; 
     712 
    694713        if(CURRENCY_ACCURACY!=0) 
    695714                currency+=DECIMAL_SYMBOL+lessthanone; 
     715                 
    696716        return currency; 
     717         
    697718} 
    698719 
  • trunk/phpbms/modules/bms/javascript/invoice.js

    r360 r361  
    10781078         
    10791079        //calculate and reformat discount 
    1080         var numDiscount,discountValue
     1080        var numDiscount
    10811081        if(discountFromID.value=="" || discountFromID.value=="0" || discountFromID.value=="0%"){ 
    10821082                numDiscount=currencyToNumber(thediscount.value); 
     
    10891089                } 
    10901090        } 
    1091         discountValue=numberToCurrency(numDiscount); 
    1092         thediscount.value=discountValue
     1091 
     1092        thediscount.value= numberToCurrency(numDiscount)
    10931093 
    10941094        //calculate totaltaxable 
Copyright © 2006-2007 Kreotek, LLC. All Rights reserved.