469,636 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

currency convert

ok, so i've my front end shop code almost working, the quantity and price all add up........ now i need to add a curency conversion function, euro (default), dollar, and uk pound.

right, i'm not sure if i should have 3 seperate functions, that do the math on the total box, and add the required, €, £ or $ icon....... it would be best to put this in one function.

ok, here's my first atempt, getting a NaN result...... i've set the values in the radio buttons.......

here's the code, hopefully you can see where i'm going wrong, its the "convert" function

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript1.2">
  3.  
  4. window.onload = addnumbers;
  5. window.onload = convert;
  6.  
  7. function convert()
  8. {
  9.  
  10. var l, t;
  11.  
  12. t=0;
  13.  
  14. for (l=0; l<document.form1.currency.length;l++)
  15.     {
  16.         if (document.form1.currency[l].checked)
  17.         {
  18.             t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
  19.         }
  20.     }
  21.  
  22.     document.form1.totalbox.value = t;
  23. }
  24.  
  25.  
  26. function checkreset()
  27. {
  28.  
  29.    if (confirm('Are you sure you wish to clear the form?'))
  30.    {
  31.       return true
  32.    }
  33.    return false
  34. }
  35.  
  36. //function addshipping()
  37.  
  38. //{
  39.  
  40. //}
  41.  
  42. function addnumbers()
  43. {
  44.  
  45. var a, b, c, d, e, f, g, h, v, j, s, i;
  46.  
  47. // This function reads the values from the input boxes
  48. // parseInt converts the input boxes into an integer
  49. a = parseInt(document.form1.abox.value);
  50. b = parseInt(document.form1.bbox.value);
  51. c = parseInt(document.form1.cbox.value);
  52. d = parseInt(document.form1.dbox.value);
  53. e = parseInt(document.form1.ebox.value);
  54. f = parseInt(document.form1.fbox.value);
  55. g = parseInt(document.form1.gbox.value);
  56. h = parseInt(document.form1.hbox.value);
  57. v = parseInt(document.form1.ibox.value);
  58. j = parseInt(document.form1.jbox.value);
  59.  
  60.  
  61. //sets default value of shipping to 0, then
  62. //loops through the shipping radio boxes to get value
  63.  
  64.     s=0;
  65.  
  66.  
  67.     for (i=0; i<document.form1.shipping.length;i++)
  68.     {
  69.         if (document.form1.shipping[i].checked)
  70.         {
  71.             s = parseInt(document.form1.shipping[i].value);
  72.         }
  73.     }
  74.  
  75.  
  76. // Then we do the math
  77. result = a + b + c + d + e + f + g + h + v + j;
  78.  
  79.  
  80. // This adds the total number of products to the quantity box
  81. document.form1.qbox.value = result;
  82.  
  83. // This calculates the total price, including vat at 21% and adds the shipping cost
  84. // toFixed fixes the decimal point 2 places
  85.  
  86.  
  87. document.form1.totalbox.value = "€" + (s+result*10*21/100+result*10).toFixed(2);
  88.  
  89.  
  90.  
  91.  
  92.  
  93. // As the function has already done the output, we don’t need a return value
  94. }
  95. </script>
  96.  
  97.  
Nov 1 '07 #1
9 2320
acoder
16,027 Expert Mod 8TB
Use the default (euro) as the standard, i.e. 1.00 and then get the currency rates for the pound and the dollar based on the euro.

Note that this should not really be done using JavaScript because the values change all the time. You should be using server-side code to get real-time rates.

However, if using as an estimate it may suffice as long as you update it often enough or, better still, get the data while loading the page (using server-side code).
Nov 1 '07 #2
t's just a project, so i can have fixed rates. i have the rates for euro and the dollar.

the default price apears in a text box in euros.

i have this function i'm using to convert that value, but it's not working

Expand|Select|Wrap|Line Numbers
  1. function convert()
  2. {
  3.  
  4. var l, t;
  5.  
  6. t=0;
  7.  
  8. for (l=0; l<document.form1.currency.length;l++)
  9.     {
  10.         if (document.form1.currency[l].checked)
  11.         {
  12.             t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
  13.         }
  14.     }
  15.  
  16.     document.form1.totalbox.value = t;
  17. }
  18.  


and the relevant html code

Expand|Select|Wrap|Line Numbers
  1. <table width="100%" border="0" cellpadding="6" cellspacing="1" bgcolor="#FFF7D2">
  2.         <tr>
  3.           <td colspan="3" bgcolor="#FFF7D2"><span class="style3">Please choose your currency, default is in euros. </span></td>
  4.         </tr>
  5.         <tr>
  6.           <td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>Euro</strong> </span><br />
  7.             <input name="currency" type="radio" onclick="convert()" value="0" /></td>
  8.           <td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>US Dollar</strong><br />
  9.             </span>
  10.               <input type="radio" name="currency" onclick="convert()" value=".69" /></td>
  11.           <td width="33%" align="center" bgcolor="#FFFFFF"><span class="style1"><strong>UK Pound </strong><br />
  12.             </span>
  13.               <input type="radio" name="currency" onclick="convert()" value="1.44" /></td>
  14.         </tr>
  15.       </table>
  16.  
Nov 1 '07 #3
acoder
16,027 Expert Mod 8TB
Use 1 instead of 0 for the euro value and use parseFloat instead of parseInt because you will be dealing with non-integer values.
Nov 1 '07 #4
thats still not working, it has to be something wrong with this code, i'm pretty new to this so, what i'm trying to do is get the value of the total box, multiply it by the cosen numerical value on the currency radio buttons.

code

Expand|Select|Wrap|Line Numbers
  1. function convert()
  2. {
  3.  
  4. var l, t;
  5.  
  6. t=0;
  7.  
  8. for (l=0; l<document.form1.currency.length;l++)
  9.     {
  10.         if (document.form1.currency[l].checked)
  11.         {
  12.             t = parseInt(document.form1.currency[l].value) * parseInt(document.form1.totalbox.value);
  13.         }
  14.     }
  15.  
  16.     document.form1.totalbox.value = t;
  17. }
Nov 1 '07 #5
acoder
16,027 Expert Mod 8TB
You've not changed parseInt to parseFloat.

When you say it's not working, what does it mean? Errors or nothing?
Nov 1 '07 #6
You've not changed parseInt to parseFloat.

When you say it's not working, what does it mean? Errors or nothing?
sorry, changed that, parseFloat does the same thing.

what happens is... the text box, called totalbox, holds the value for the quantity of items picked, including the shipping cost.

when you select one of the radio buttons, ie the dollar one, it turns the amount in the total box to NaN, i'm asuming this line is causing the problem...

Expand|Select|Wrap|Line Numbers
  1. t = parseFloat(document.form1.totalbox.value) * parseFloat(document.form1.currency[l].value);
Nov 1 '07 #7
acoder
16,027 Expert Mod 8TB
Change the value of the dollar radio button to 0.69.

Another thing you should do is use the calculated total value, not the value in the totals box.
Nov 1 '07 #8
Change the value of the dollar radio button to 0.69.

Another thing you should do is use the calculated total value, not the value in the totals box.
i've changed to 0.69, still the same.

i dont really understand your second point, could you clarify please.

the total value is the value that apears in the totals box....

Expand|Select|Wrap|Line Numbers
  1. document.form1.totalbox.value = "€" + (s+result*10*21/100+result*10).toFixed(2);
Nov 2 '07 #9
acoder
16,027 Expert Mod 8TB
i dont really understand your second point, could you clarify please.

the total value is the value that apears in the totals box...
It is, but when you click on a radio button and it changes to another currency and then decide to change to another currency, the changed currency total value is used to multiply with the currency rate giving incorrect results.
Nov 2 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Marcelo López | last post: by
2 posts views Thread by Willing 2 Learn | last post: by
3 posts views Thread by Dalan | last post: by
reply views Thread by R. John Reed | last post: by
7 posts views Thread by meenasamy | last post: by
14 posts views Thread by Scott M. | last post: by
4 posts views Thread by bernyman | last post: by
25 posts views Thread by mereba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.