473,770 Members | 6,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

currency convert

47 New Member
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 2575
acoder
16,027 Recognized Expert Moderator MVP
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
ballygowanboy
47 New Member
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 Recognized Expert Moderator MVP
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
ballygowanboy
47 New Member
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 Recognized Expert Moderator MVP
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
ballygowanboy
47 New Member
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 Recognized Expert Moderator MVP
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
ballygowanboy
47 New Member
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 Recognized Expert Moderator MVP
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
2261
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 add a new attributte in a xml's node I am converting it from currency to decimal using decimal.parse, but i cannot convert from the result in decimal to the current type again. Ideas ??
2
574
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 multiply(by int)& show, have a constructor w/ & w/out arguments. Header file should have private data & all 6 functions from above.Class definition file should implement my ADT class. What I have so far: Main program #include "jahcurrency.h" #include...
3
9421
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 price of item field (say $49.95), and a percentage discount field (say 10% = $5.00), and calculated net cost field of the two. Access seemingly doesn't understand banker's rules as the resulting total is $44.96. Could this be a bug? Why does it...
0
8430
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", System.Globalization.NumberSty­les.Currency));
7
4071
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 return the new amount in the needed currency, I need this function to work real time (obtaining real time currency exchange rates), any ideas???
14
4361
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 unformatted data values (as decimals) just fine, so I know there's not a problem with the data retrieval, just the formatting. I have read that this would work: lblPrice.Text = prodRow.ToString("C");
16
9528
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 currency; var amountEntered; currency = window.prompt('Please enter 0 to convert from Dollars to euros and 1 to convert form euros to dollars',''); currency = parseFloat(currency); while (currency < 0 || currency > 1) { currency =...
4
2092
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 <stdio.h> int main (void) {
25
5953
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 calculator- looking interface. I'm quite new to C++, and I would need some help. Please any suggestion is welcome :) The following is what I have been able to do so far:
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.