SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How does this javascript work?

    I am trying to work out how this calculates the final value for the currency-what does this code do please: if (mDe=='USD') fatorDe = 2.139817;


    Thank you

    Code:
    <SCRIPT language=javascript>
    
    function formatCurrency(num) {
    num = num.toString().replace('.','');
    num = num.toString().replace(',','.');
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num1 = Math.floor(num);
    cents = num-num1;
    cents = Math.floor(cents*10000+0.50000000001);
    cents = cents/10000;
    num=num1.toString();
    ct = cents.toString()
    if (cents>0) ct = ct.substring(2,ct.length+1);
    if(ct.length<2)
    ct = ct + "0";
    if(ct.length<2)
    ct = ct + "0";
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+'.'+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + ',' + ct);
    }
    
    
    function popupBGcom (a,b,x,y) {
    	window.open (a,b,'width=' + x + ',height=' + y + ',resizable=0,status=0,menubar=0,scrollbars=1');
    }    
    
    
    function repetir (a ,b) {
    	a.value = b.value;
    }    
    
    function repetirComMoeda (a ,b, c , d) {
    	a.value = b.value;
    	c.value = d.value;
    }    
    
    //  End -->
    </SCRIPT>
    <!-- funcoes em java -->
    <SCRIPT language=javascript>
    <!--
    
    function cValor(a){
    
    	b = a.toString().replace(/\./g,'');
    	b = b.toString().replace(/\,/g,'.');
    
    	return parseFloat(b);
    
    }
    
    function formatCurrencyIng(num) {
    
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num1 = Math.floor(num);
    cents = num-num1;
    cents = Math.floor(cents*10000+0.50000000001);
    cents = cents/10000;
    num=num1.toString();
    ct = cents.toString()
    if (cents>0) ct = ct.substring(2,ct.length+1);
    if(ct.length<2)
    ct = ct + "0";
    if(ct.length<2)
    ct = ct + "0";
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+'.'+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + ',' + ct);
    
    }
    
    
    //  End -->
    </SCRIPT>
    
    
    <SCRIPT language=javascript>
    <!--
    function calculaResultado() {
    
    	mDe = moedaDe.value;
    
    	if (mDe=='BRL') fatorDe = 1;
    if (mDe=='USD') fatorDe = 2.139817;
    if (mDe=='EUR') fatorDe = 2.830276;
    if (mDe=='ZAR') fatorDe = 0.256344;
    if (mDe=='ARS') fatorDe = 0.576777;
    if (mDe=='AUD') fatorDe = 1.589899;
    if (mDe=='CAD') fatorDe = 1.825343;
    if (mDe=='XCD') fatorDe = 0.786205;
    if (mDe=='CLP') fatorDe = 0.003749;
    if (mDe=='CNY') fatorDe = 0.312953;
    if (mDe=='COP') fatorDe = 0.000951;
    if (mDe=='CUP') fatorDe = 2.128443;
    if (mDe=='DKK') fatorDe = 0.37952;
    if (mDe=='EGP') fatorDe = 0.377232;
    if (mDe=='ECS') fatorDe = 0.000085;
    if (mDe=='HUF') fatorDe = 0.010031;
    if (mDe=='INR') fatorDe = 0.042759;
    if (mDe=='IDR') fatorDe = 0.000203;
    if (mDe=='ILS') fatorDe = 0.513571;
    if (mDe=='JMD') fatorDe = 0.023893;
    if (mDe=='JPY') fatorDe = 0.021777;
    if (mDe=='MAD') fatorDe = 0.255477;
    if (mDe=='MXN') fatorDe = 0.161964;
    if (mDe=='NOK') fatorDe = 0.325297;
    if (mDe=='NZD') fatorDe = 1.240147;
    if (mDe=='PYG') fatorDe = 0.000422;
    if (mDe=='PEN') fatorDe = 0.720579;
    if (mDe=='GBP') fatorDe = 3.220342;
    if (mDe=='CZK') fatorDe = 0.105858;
    if (mDe=='RUB') fatorDe = 0.065055;
    if (mDe=='SEK') fatorDe = 0.268817;
    if (mDe=='CHF') fatorDe = 1.883762;
    if (mDe=='THB') fatorDe = 0.060435;
    if (mDe=='TWD') fatorDe = 0.064028;
    if (mDe=='UYU') fatorDe = 0.089096;
    
    
    	mPara = moedaPara.value;
    
    	if (mPara=='BRL') fatorPara = 1;
    if (mPara=='USD') fatorPara = 2.139817;
    if (mPara=='EUR') fatorPara = 2.830276;
    if (mPara=='ZAR') fatorPara = 0.256344;
    if (mPara=='ARS') fatorPara = 0.576777;
    if (mPara=='AUD') fatorPara = 1.589899;
    if (mPara=='CAD') fatorPara = 1.825343;
    if (mPara=='XCD') fatorPara = 0.786205;
    if (mPara=='CLP') fatorPara = 0.003749;
    if (mPara=='CNY') fatorPara = 0.312953;
    if (mPara=='COP') fatorPara = 0.000951;
    if (mPara=='CUP') fatorPara = 2.128443;
    if (mPara=='DKK') fatorPara = 0.37952;
    if (mPara=='EGP') fatorPara = 0.377232;
    if (mPara=='ECS') fatorPara = 0.000085;
    if (mPara=='HUF') fatorPara = 0.010031;
    if (mPara=='INR') fatorPara = 0.042759;
    if (mPara=='IDR') fatorPara = 0.000203;
    if (mPara=='ILS') fatorPara = 0.513571;
    if (mPara=='JMD') fatorPara = 0.023893;
    if (mPara=='JPY') fatorPara = 0.021777;
    if (mPara=='MAD') fatorPara = 0.255477;
    if (mPara=='MXN') fatorPara = 0.161964;
    if (mPara=='NOK') fatorPara = 0.325297;
    if (mPara=='NZD') fatorPara = 1.240147;
    if (mPara=='PYG') fatorPara = 0.000422;
    if (mPara=='PEN') fatorPara = 0.720579;
    if (mPara=='GBP') fatorPara = 3.220342;
    if (mPara=='CZK') fatorPara = 0.105858;
    if (mPara=='RUB') fatorPara = 0.065055;
    if (mPara=='SEK') fatorPara = 0.268817;
    if (mPara=='CHF') fatorPara = 1.883762;
    if (mPara=='THB') fatorPara = 0.060435;
    if (mPara=='TWD') fatorPara = 0.064028;
    if (mPara=='UYU') fatorPara = 0.089096;
    
    
    	a = valor.value;
    	valorN = cValor(a);
    
    	res = valorN * fatorDe / fatorPara;
    	res = Math.round(100 * res ) / 100;
    	resS = formatCurrencyIng(res);
    
    	resultado.innerText= 'Resultado: $ ' + valor.value + ' ' + moedaDe.options[moedaDe.selectedIndex].text  + ' = $ ' + resS  + ' ' + moedaPara.options[moedaPara.selectedIndex].text;
    
    
    }    
    
    
    //  End -->
    </SCRIPT>
    
    
    
    
    <p>
    $ 
    <INPUT onblur=this.value=formatCurrency(this.value) 
    onfocus="resultado.innerText=''" value=100,00 size=15 name=valor> 
    
    <SELECT onchange="resultado.innerText=''" name=moedaDe>
    <OPTION value=BRL>real(is), Brasil</OPTION>
    <OPTION selected value=USD>dólar(es), EUA</OPTION>
    <OPTION value=EUR>euro(s), CE</OPTION>
    <OPTION value=ZAR>rande(s), África do Sul</OPTION>
    <OPTION value=ARS>peso(s), Argentina</OPTION>
    <OPTION value=AUD>dólar(es), Austrália</OPTION>
    <OPTION value=CAD>dólar(es), Canadá</OPTION>
    <OPTION value=XCD>dólar(es), Caribe</OPTION>
    <OPTION 
                              value=CLP>peso(s), Chile</OPTION><OPTION 
                              value=CNY>yuan(s), China</OPTION><OPTION 
                              value=COP>peso(s), Colômbia</OPTION><OPTION 
                              value=CUP>peso(s), Cuba</OPTION><OPTION 
                              value=DKK>coroa(s), Dinamarca</OPTION><OPTION 
                              value=EGP>libra(s), Egito</OPTION><OPTION 
                              value=ECS>sucre(s), Equador</OPTION><OPTION 
                              value=HUF>forint(s), Hungria</OPTION><OPTION 
                              value=INR>rúpia(s), Índia</OPTION><OPTION 
                              value=IDR>rúpia(s), Indonésia</OPTION><OPTION 
                              value=ILS>shekel(s), Israel</OPTION><OPTION 
                              value=JMD>dólar(es), Jamaica</OPTION><OPTION 
                              value=JPY>yen(es), Japão</OPTION><OPTION 
                              value=MAD>dirham(s), Marrocos</OPTION><OPTION 
                              value=MXN>peso(s), México</OPTION><OPTION 
                              value=NOK>coroa(s), Noruega</OPTION><OPTION 
                              value=NZD>dólar(es), Nova Zelândia</OPTION><OPTION 
                              value=PYG>guarani(s), Paraguai</OPTION><OPTION 
                              value=PEN>sol(is), Peru</OPTION><OPTION 
                              value=GBP>libra(s), Reino Unido</OPTION><OPTION 
                              value=CZK>coroa(s), Rep. Tcheca</OPTION><OPTION 
                              value=RUB>rublo(s), Rússia</OPTION><OPTION 
                              value=SEK>coroa(s), Suécia</OPTION><OPTION 
                              value=CHF>franco(s), Suíça</OPTION><OPTION 
                              value=THB>bath(s), Tailândia</OPTION><OPTION 
                              value=TWD>dólar(es), Taiwan</OPTION><OPTION 
                              value=UYU>peso(s), Uruguai</OPTION></SELECT></p>
          
                <p>
    <SELECT onchange="resultado.innerText=''" name=moedaPara>
    <OPTION selected value=BRL>real(is), 
                              Brasil</OPTION><OPTION value=USD>dólar(es), 
                              EUA</OPTION><OPTION value=EUR>euro(s), 
                              CE</OPTION><OPTION value=ZAR>rande(s), África do 
                              Sul</OPTION><OPTION value=ARS>peso(s), 
                              Argentina</OPTION><OPTION value=AUD>dólar(es), 
                              Austrália</OPTION><OPTION value=CAD>dólar(es), 
                              Canadá</OPTION><OPTION value=XCD>dólar(es), 
                              Caribe</OPTION><OPTION value=CLP>peso(s), 
                              Chile</OPTION><OPTION value=CNY>yuan(s), 
                              China</OPTION><OPTION value=COP>peso(s), 
                              Colômbia</OPTION><OPTION value=CUP>peso(s), 
                              Cuba</OPTION><OPTION value=DKK>coroa(s), 
                              Dinamarca</OPTION><OPTION value=EGP>libra(s), 
                              Egito</OPTION><OPTION value=ECS>sucre(s), 
                              Equador</OPTION><OPTION value=HUF>forint(s), 
                              Hungria</OPTION><OPTION value=INR>rúpia(s), 
                              Índia</OPTION><OPTION value=IDR>rúpia(s), 
                              Indonésia</OPTION><OPTION value=ILS>shekel(s), 
                              Israel</OPTION><OPTION value=JMD>dólar(es), 
                              Jamaica</OPTION><OPTION value=JPY>yen(es), 
                              Japão</OPTION><OPTION value=MAD>dirham(s), 
                              Marrocos</OPTION><OPTION value=MXN>peso(s), 
                              México</OPTION><OPTION value=NOK>coroa(s), 
                              Noruega</OPTION><OPTION value=NZD>dólar(es), Nova 
                              Zelândia</OPTION><OPTION value=PYG>guarani(s), 
                              Paraguai</OPTION><OPTION value=PEN>sol(is), 
                              Peru</OPTION><OPTION value=GBP>libra(s), Reino 
                              Unido</OPTION><OPTION value=CZK>coroa(s), Rep. 
                              Tcheca</OPTION><OPTION value=RUB>rublo(s), 
                              Rússia</OPTION><OPTION value=SEK>coroa(s), 
                              Suécia</OPTION><OPTION value=CHF>franco(s), 
                              Suíça</OPTION><OPTION value=THB>bath(s), 
                              Tailândia</OPTION><OPTION value=TWD>dólar(es), 
                              Taiwan</OPTION><OPTION value=UYU>peso(s), 
                              Uruguai</OPTION></SELECT></p>
                      
    <BR><INPUT onclick=calculaResultado() value="Converter >>" type=button name=botao>
    <p><A class=normalB name=resultado>&nbsp;</A>

  2. #2
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bayliss Trevor View Post
    I am trying to work out how this calculates the final value for the currency-what does this code do please: if (mDe=='USD') fatorDe = 2.139817;
    Its a really silly way of tying in a currency symbol (USD) with a conversion factor.

    mDe is a variable that is initialised with the currency symbol in the html of the page and instead of being sensible and using arrays the author has written a huge set of if-then statements.

    mDe = moedaDe.value; - somewhere on the page there will be a form control box with the id element set to moedaDe (which probably means something in portuguese)

    mDe has its value set from there.

    Literally the phrase you have picked out is if mDe contains the characters USD then set the variable fatorDe (which is the conversion factor) to the value 2.139817

    A similar process is gone through for fatorPara which is a second currency.

    The amount entered to convert is found here:

    a = valor.value;
    valorN = cValor(a);

    A text box (presumably) on the page with its id set to valor is used to load a variable which is then passed through a filter function called cValor to make sure it is formatted correctly before its value gets assigned to valorN

    Later on you get

    res = valorN * fatorDe / fatorPara;

    Looking at the numbers fatorDe must be the TO currency conversion figure and fatorPara is the from.

    So for example you want to convert from GBP to USD looking up on the tables in the program fatorDe for USD is 3.22 and fatorPara for GBP is 2.13. Lets say you want to convert ten pounds to dollars, then valorN will be 10.

    So 3.22 divided by 2.13 is 1.511. Multiply this by 10 and you get 10GBP = 15.11USD.

    Clearer?

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for that explanation Martin. Yes it seems a silly way of doing things as you say.
    Looking at the numbers fatorDe must be the TO currency conversion figure and fatorPara is the from.
    What are these TO and FROM values-actual exchange rates? Thank you

  4. #4
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bayliss Trevor View Post
    Thank you for that explanation Martin. Yes it seems a silly way of doing things as you say.


    What are these TO and FROM values-actual exchange rates? Thank you
    Nah they are just conversions from a common base, Otherwise you would have to have a whole table for EVERY currency instead of just two tables.

    Is a ratio thing.

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Martin how do i put those if statements in an array?
    Last edited by Bayliss Trevor; May 10, 2009 at 14:58.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Looking at the numbers fatorDe must be the TO currency conversion figure and fatorPara is the from.
    Correct. I suspect this code was written by a Spanish speaker. "De" means To and "Para" is From.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Correct. I suspect this code was written by a Spanish speaker. "De" means To and "Para" is From.
    Lol.. yeah I can narrow that down a bit -maybe the coder was Brazilian cos there are brazilian portuguese bits in there too.

  8. #8
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bayliss Trevor View Post
    Thanks Martin how do i put those if statements in an array?
    If I was coding this I would go to XE and get a currency table in USD then put the table into a single array. I would then convert from any currency to any other by first converting into USD then converting the USD into any other currency.

    If I was worried about currency fluctuations then I would build a PHP program that extracted the table once a day from XE and posted the values into the Javascript ... or just work it all in php.

  9. #9
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies I used php to do this before but now I am going the pure js route. I do have a list of currency exchange rates which I get updated from a webservice.
    So I now want to set up an array something like this:
    var faq = new Array(3)
    faq[0] = "USD"
    faq[1] = "CAD"
    faq[2] = "EUROS"

    This is my first attempt at arrays in javascript!


Bookmarks

Posting Permissions

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