473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2546
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

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

Similar topics

0
by: Marcelo López | last post by:
Hello everybody. I'm working with an office pivot table and i have to convert from its currency type to a decimal (in C#) , operate with the result and then re-convert it to the currency type to...
2
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar...
3
by: Dalan | last post by:
Is there any code available to address currency rounding problems in Access 97? Apparently, selecting currency type table fields does not resolve the problem. For instance, in my form I have a...
0
by: R. John Reed | last post by:
Hi All, I'm am looking to convert a currency string (e.g. "$1,234.56" to a double value). It appears this will work: double val = Convert.ToDouble(Double.Parse­("$123,456.78901",...
7
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and...
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
16
by: xjohnx | last post by:
Hi, I'm hoping someone can help me I am quite new to Javascript and have had to create a programme which converts dollars into euros and vice versa, here is my script which is working var...
4
by: bernyman | last post by:
I have the following program and I need to add error handling to prevent negative numbers and letters being entered when this program is executed. Any help is appreciated. #include...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.