JavaScript - Currency
Hello
I have this script Code: /* This script and many more are available free online at The JavaScript Source!! http://javascript.internet.com Created by: Mario Costa | */ function currencyFormat(fld, milSep, decSep, e) { var sep = 0; var key = ''; var i = j = 0; var len = len2 = 0; var strCheck = '0123456789'; var aux = aux2 = ''; var whichCode = (window.Event) ? e.which : e.keyCode; if (whichCode == 13) return true; // Enter if (whichCode == 8) return true; // Delete key = String.fromCharCode(whichCode); // Get key value from key code if (strCheck.indexOf(key) == -1) return false; // Not a valid key len = fld.value.length; for(i = 0; i < len; i++) if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break; aux = ''; for(; i < len; i++) if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i); aux += key; len = aux.length; if (len == 0) fld.value = ''; if (len == 1) fld.value = '0'+ decSep + '0' + aux; if (len == 2) fld.value = '0'+ decSep + aux; if (len > 2) { aux2 = ''; for (j = 0, i = len - 3; i >= 0; i--) { if (j == 3) { aux2 += milSep; j = 0; } aux2 += aux.charAt(i); j++; } fld.value = ''; len2 = aux2.length; for (i = len2 - 1; i >= 0; i--) fld.value += aux2.charAt(i); fld.value += decSep + aux.substr(len - 2, len); } return false; } and I call it like that: onKeyPress="return(currencyFormat(this,',',',',event))" the ouput is: 100,000.00 How can I get rid of of the last 2 ziros after the last dot I need this kind of output 1,000 10,000 100,000 1,000,000 any help ? Similar TutorialsHi I have the following currency converter code which works fine, but I need to make it possible for someone else to edit the euro price using a visual editor such as contribute. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script language='javascript'> exchRate = 1.13; function getUKP(price) { document.write('(£' + Math.round(price / exchRate) + ')'); } </script> </head> <body> <strong>€526</strong> <script type="text/javascript">getUKP('526');</script><br /><br /> <strong>€1999</strong> <script type="text/javascript">getUKP('1999');</script> </body> </html> This code requires someone to change the figure in the javascript as well as the html. I guess I need to somehow automatically pick up the numeric characters only from the html which appear on the same line of code... or perhaps there's an easier way.? Any help would be much appreciated. Thanks We format euro currency as 1.000.000,00 (just the opposite of USD). Anyone who could give me a script that changes the input into this format? I tried 'replace' with a regular expression, but my regexes don't work. Hey guys. I am in an introductory computer science class and I am having trouble writing JavaScript code for a currency converter. I can get it to where it spits out numbers, but its always the same numbers. I think there's something wrong in the calculations part of my code but I can't figure it out. I am posting it below, and any help would be greatly appreciated. Thank you. <html> <head> <title>CURRENCY CONVERTER</title> <script language="JavaScript"> function converting() { with (document.conversions) { if (unit[1].selected) { var result = eval(unit.value*0.974); convertedValue.value = result; } // end if if (unit[2].selected) { var result = eval(unit[2].value*0.492); convertedValue.value = result; } // end if if (unit[3].selected) { var result = eval(unit[3].value*0.706); convertedValue.value = result; } // end if } // end with } // end function converting </script> </head> <form name = "conversions"> <table border=6 bgcolor ="yellow"> <tr> <td><font color="red" size=6>Enter amount in U.S. $:</font></td> <td><input type="text" name="inUnit" size = 4></td> </tr> <tr> <td><font color="red" size=6>Select target unit:</font> <td><select name="unit"> <option value="Select Currency" selected>Choose one... <option value="1">Canadian Dollars <option value="2">British Pounds <option value="3">Euros </select> <td><input type="button" value="Convert" onclick="converting()"> </tr> <tr> <td><font color="green" size=6>Converted Value:</font> <td><input type="text" name="convertedValue" size = 4> <td><input type="reset" value="Reset"> </tr> </table> </form> </body> </html> Hi, I'm really stupid and haven't been working with javascript long, so forgive my incompetence. I need to create a simple currency convertor. Not with live rates or anything like that. It needs to allow a user to input a dollar amount, add a bank fee to that and convert the amount into one of six currencies. here is my code Code: <html> <head> <script type="text/javascript"> <!-- var Amount=document.getElementById('amount'); var Fees=document.getElementById('bankComm'); var Total=document.getElementById('ausTotal'); var Currency=document.getElementById('currency'); var Converted=document.getElementById('converted'); var Choice=document.getElementById('choice'); function fees() { var Amount=document.getElementById('amount'); var Fees=document.getElementById('bankComm'); if (Amount.value>=1&&Amount.value<5001) { Fees.value=20 } if (Amount.value>=5001&&Amount.value<10001) { Fees.value=30 } if (Amount.value>=10001) { Fees.value=40 } } function addFees() { var Amount=document.getElementById('amount'); var Fees=document.getElementById('bankComm'); var Total=document.getElementById('ausTotal'); Total.value=Fees.value+Amount.value } function convert() { var Amount=document.getElementById('amount'); var Currency=document.getElementById('currency'); var Converted=document.getElementById('converted'); var Choice=document.getElementById('choice'); var US=0.91810; var UK=0.60285; var HK=7.12812; var SNG=1.26784; var JY=84.2563; var NZ=1.29565; switch(document.convertor.currency.value) { case:"US Dollars" document.convertor.converted.value=US*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break case:"UK Ponds" document.convertor.converted.value=UK*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break case:"HK Dollars" document.convertor.converted.value=HK*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break case:"Singapore Dollars" document.convertor.converted.value=SNG*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break case:"Japanese Yen" document.convertor.converted.value=JY*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break case:"NZ Dollars" document.convertor.converted.value=NZ*document.convertor.amount.value document.convertor.choice.value=document.convertor.currency.value break } } //--> </script> </head> <body> <div id="divWrapper"> <form name="Convertor" id="Convertor"> Enter amount in Australian Dollars $ <input name="amount"type="text" id="amount" onBlur="fees()" value="0" size="7" /> + $<input name="bankComm" type="text" id="bankComm" style="border:0px" onChange="addFees()" value="0" size="2" /> administration charges = <input name="ausTotal" type="text" id="ausTotal" style="border:0px" value="0" size="7"> <br /><br /> Please select a currency <select name="currency" id="currency"> <option>Please Choose One</option> <option value="US Dollars">US Dollars</option> <option value="UK Pounds">UK Pounds</option> <option value="HK Dollars">HK Dollars</option> <option value="Singapore Dollars">Singapore Dollars</option> <option value="Japanese Yen">Japanese Yen</option> <option value="NZ Dollars">NZ Dollars</option> </select><br /><br /> The amount is: <input name="converted" type="text" id="converted" value="" size="7"/> in <input name="choice" type="text" id="choice" style="border:0px" value=""> <br /><br /> <input name="convert" type="button" id="convert" onClick="convert()" value="Convert" /> </form> </div> </body> </html> I've tried using getElementbyId and using the names, as far as I can tell, it's sytactically correct, but It doesn't seem to work no matter what I do. I have a headache, actually I have two. If anyone can help me, I'd be extremely grateful. Dear All, On my HTML page, I am trying to use as a snippet the script for automatic currency conversion. It is well known that exchange rates between currency are being changed very often (most of times even daily) and I don't want to republish either page with script or entire website every time the exchange rate changes. For website design I am using xsitepro software because I don't know programming. So what I would like to have on specific page of website is something similar to table (if anyone has any better recommendation than using a table, please share). In this table I would like to state prices of my services. Primary currency is only one. So in this column, the numbers shouldn't be changed unless I want to change the prices (and of course therefore republish the website, no need to republish sitemap). In the rest of the columns the numbers should be changed immediately, without needing to republish anything (thats the point of the script) and also rounded to each 5. What I mean with ''each 5'' is shown on few of the following random chosen examples (1500): 1500.03 is rounded to 1500 1500.30 is rounded to 1500 1500.50 is rounded to 1500 1502.49 is rounded to 1500 1502.50 is rounded to 1505 (!) 1504.99 is rounded to 1505 1505.01 is rounded to 1505 1507.49 is rounded to 1505 1507.50 is rounded to 1510 and so on. Idea is to prevent from getting coins. So the break point is on 2.50, 7.50, 12.50, 17.50, 22.50, 27.50 and so on. Not sure how to mathematically describe this. Hopefully I was understandable. I want to have in the columns only the currencies that I am willing to accept if potential client cannot change the currency to my primary one (given in second most left column) in his local exchange office. But I repeat that what Im trying to do is having automatically and immediately updated numerical values on particular HTML page based on most recent exchange rates (of course trustful source is needed). Since I don't know programming, I did some research on google and discovered this: http://coinmill.com/webmaster_options.html The downer script on the right side looks ok but its not even close to what I need due to five reasons: - it is being converted outside the table - with such way i cannot clearly show which currency is primary (prefered) one - conversion is being done to only one other currency at the same time - no rounding to ''each 5'' - unknown source (and therefore untrustful one) of updating most recent exchange rates Table such as this one: http://www.x-rates.com/ looks great but still not meeting my requirements. I would like to replace flags in the left column with ''service 1'', ''service 2'', ''product 1'' etc. Also I have no idea how rounding could be done. So at the end I would end up with something like (here Im showing only two ''still willing to accept'' currencies just to show an example): Currency:Euro Currency:American Dollar Currency:Japanese Yen Service1 500 670 51535 Service2 400 535 41228 Service3 200 265 20614 Top left corner as it is on x-rates.com would be even better. note: when I previewed my post before submitting, I noticed that the forum doesn't let me make so much spaces but I think those numbers are still understandable where do they belong. Any help would be muuuuuch appricated Hey I've been working on my Spanish Clubs' website over the summer and I need some help when making this converter. I want to make it so when someone chooses a different country, it will change the answeres and do the equation in the text boxes and also if they change the numbers manually they will also do the equation. I've tried it out and it's making me go mad. PLEASE HELP! Here is the javascript code I made: Code: function calculatecountry() { var country = calculate.countrys.options[calculate.countrys.selectedIndex].text; //===============================COUNTRIES==================================== if (countrys=="--Country--") { var usd1 = 0; var other1 = 0; } if (countrys=="Argentina") { var usd1 = 0.2553; var other1 = 1; } if (countrys=="Bolivia") { var usd1 = 0.14245; var other1 = 1; } if (countrys=="Chile") { var usd1 = 0.0018; var other1 = 1; } //================================EQUATIONS===================================== var c1 = calculate.other.value; var c2 = calculate.usd.value; var ans1 = c1 * other1; var ans2 = c2 * usd1; calculate.usd.value = ans1; calculate.other.value = ans2; } Here is the form code: Code: <html><head><title></title> <script type="text/javascript" src="js_money_calc.js"></script> </head><body> Converter <form name="calculate"> <select name="countrys" onChange="calculatecountry()"> <option selected>--Country--</option> <option>Argentina</option> <option>Bolivia</option> <option>Chile</option> </select><p> <input type="text" name="usd" onKeyUp="calculatecountry()" /> = <input type="text" name="other" onKeyUp="calculatecountry()" /></p> </form> </body></html> Thanks. ... or at least adding the commas? I need to format this number as currency. It is within the "flot" code, which is a charting framwork. I dont know anything about javascript at all so bear with me. Here is part of the code, the piece in red calls for the number. This is what I need displayed with commas, so its easy to read. I have found a few examples on the web but I have no idea how to apply them to this code. Can anyone help? Code: var previousPoint = null; $("#placeholder").bind("plothover", function (event, pos, item) { $("#x").text(pos.x.toFixed(2)); $("#y").text(pos.y.toFixed(2)); if ($("#enableTooltip:checked").length > 0) { if (item) { if (previousPoint != item.datapoint) { previousPoint = item.datapoint; $("#tooltip").remove(); var x = item.datapoint[0].toFixed(2), y = item.datapoint[1].toFixed(2); showTooltip(item.pageX, item.pageY, '<b>' + item.series.label + '</b> '+ ": " + y ); Hello, Firstly - relatively new to javascript. I have seen many examples of code that will return a passed value in a currency format - however I have a sales form that is calculating values via a function in the text boxes I want formatted with a currency ($) symbol. The function that is called is not passing any values, and returning a calculation of the form subtotals per item, and then total sales figure. Can anybody give me any hints on format these cells to a currency value? I can give examples of how my code is currently if required. Thanks in advance Hi all, Im in the process of creating a auction site for charity, im working on the last page which allows the user to enter a bid i have this javascript function that i have been working on to try and achieve the following. the user enters a bid use javascript to check to make sure its greater then and not equal to the current price simple right? But no its not well as im new to this maybe iv got lost in the code some where heres my code Code: function IsNewBidGreater(windowname) { var NewBid; var CurrentPrice; CurrentPrice = document.getElementById('MainContent_txtCurrentPrice').value; NewBid = document.getElementById('MainContent_txtNewBid').value; parseFloat(NewBid).toFixed(2); parseFloat(CurrentPrice).toFixed(2); if (NewBid < CurrentPrice || NewBid == CurrentPrice) { NewBid.className = "error"; document.getElementById('MainContent_ErrorLowBid').style.display = 'block'; return false; } else { document.getElementById('MainContent_ErrorLowBid').style.display = 'none'; document.getElementById('MainContent_BidConfirm').innerHTML = NewBid; ConPopUp(windowname) } } where i enter 10.50 and the current price is 9.99 i get the error message saying needs to be greater MainContent_ErrorLowBid is a hidden div on the web page which shows an error message any help would be highly appreciated (previously posted in the wrong forum and re-posted in this one) Hi there, I have scoured the internet looking for the right code but to no avail. I have gotten as far as totaling up my form but now want to format the amounts and total as currency and also want the values that = 0 to just be blank. I have tried to use the code from other solutions (posted on the net) but I just think it is over my head because I cant figure it out. I thought I was close a few times but wasn't able to bring it on home. One of my issues is using somebody's code and not knowing where in my code to place it. http://www.whackyweedremoval.com/wwr-invoice.html I have tried exhaustively to solve on my own but there is a point. Please help Anyone ever did this before? domain1.com will have a paragraph of one currency price domain2.com will have the same paragraph with a different currency price. Hi, I need to create a currency converter using a drop down menu. Firstly i needed to do a regular expressions check to make sure only numerics are entered and I got that. The one thing im pretty confused on is how to do the calculations in the actual function. Ive popped the code below, and if anyone could give me some hints on how to get the data from the drop down menu and calculate it properly that would be awesome. Thanks. -oh and i know all my coding is here but i think its relevent that you can view it all. Code: <html> <head> <style type ="text/css"> @import url(style.css); </style> <script type="text/javascript"> function convert() { var OK = true; var inDollar= document.getElementById('dollars').value; var regex= /^\d{1,}$/; if (!regex.test(inDollar)) { OK=false; alert('Australian Dollars must be entered as numeric with no decimal places'); } return OK; var rate= country.conversion.value; if (document.getElementByID('conversion').selectedIndex == 0) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' US Dollars'); } if (document.getElementByID('conversion').selectedIndex == 1) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Euro'); } if (document.getElementByID('conversion').selectedIndex == 2) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Yuan'); } if (document.getElementByID('conversion').selectedIndex == 3) { var rate= country.conversion.value; var convert = inDollar*rate; alert('$'+inDollar+' converts to'+rate+' Yen'); } } </script> </head> <body> <h1>Currency Page</h1> <form> Australian Dollars:<input type="text" name="dollars" id="dollars" /> <br /> Convert to: <select id='conversion' name='country'> <option value=".73">US Dollars</option> <option value=".62">Euro</option> <option value="5.8">Chinese Yuan</option> <option value="78.5">Japanese Yen</option> </select> <br /> <input type="button" value="Convert" onClick="convert()" /> </form> </body> </html> I can only seem to find really complicated ones online, nothing simple which i think this is. Thanks this code is formatting the price like that 500 000 but i need to price format like this 500.000 so i need dot instead space code at below PHP Code: <html> <head> <script type="text/javascript"> window.onload = attachEvents; function attachEvents() { document.getElementById('myInput').onkeyup = reformatNumber; } function reformatNumber() { // No error checking. Assumes only ever 1 DP per number var text = this.value; // Strip off anything to the right of the DP var rightOfDp = ''; var dpPos = text.indexOf('.'); if (dpPos != -1) { rightOfDp = text.substr(dpPos); text = text.substr(0, dpPos); } var leftOfDp = ''; var counter = 0; // Format the remainder into 3 char blocks, starting from the right for (var loop=text.length-1; loop>-1; loop--) { var char = text.charAt(loop); // Ignore existing spaces if (char == ' ') continue; leftOfDp = char + leftOfDp; counter++; if (counter % 3 == 0) leftOfDp = ' ' + leftOfDp; } // Strip leading space if present if (leftOfDp.charAt(0) == ' ') leftOfDp = leftOfDp.substr(1); this.value = leftOfDp + rightOfDp; } </script> </head> <body> <form> <input type="text" id="myInput" /> </form> </body> </html> Hi Guys, I'm looking for a way to convert a number in to currency format so 10000000 is 10,000,000 etc... i found this code and was hoping someone could advise how to tweak it so that it works automatically without having to hit a button i.e. I will be passing a value dynamically to the code and want it evaluated without the user clicking anything if the code was like var="10000000" <-the number I dynamically pass in and output 10,000,000 You probably guessed Javascript aint my forte. Thanks http://www.designerwiz.com/JavaScrip...ncy_format.htm Code: <!-- // == This Script Free To Use Providing This Notice Remains == // // == This Script Has Been Found In The http://www.DesignerWiz.com Javascript Public Archive Library == // // == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == // --><script language="JavaScript" type="text/javascript"> <!-- Begin function checkNum(data) { // checks if all characters var valid = "0123456789."; // are valid numbers or a "." var ok = 1; var checktemp; for (var i=0; i<data.length; i++) { checktemp = "" + data.substring(i, i+1); if (valid.indexOf(checktemp) == "-1") return 0; } return 1; } function dollarAmount(form, field) { // idea by David Turley Num = "" + eval("document." + form + "." + field + ".value"); dec = Num.indexOf("."); end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00"); Num = "" + parseInt(Num); var temp1 = ""; var temp2 = ""; if (checkNum(Num) == 0) { alert("This does not appear to be a valid number. Please try again."); } else { if (end.length == 2) end += "0"; if (end.length == 1) end += "00"; if (end == "") end += ".00"; var count = 0; for (var k = Num.length-1; k >= 0; k--) { var oneChar = Num.charAt(k); if (count == 3) { temp1 += ","; temp1 += oneChar; count = 1; continue; } else { temp1 += oneChar; count ++; } } for (var k = temp1.length-1; k >= 0; k--) { var oneChar = temp1.charAt(k); temp2 += oneChar; } temp2 = "$" + temp2 + end; eval("document." + form + "." + field + ".value = '" + temp2 + "';"); } } // End --></script> <center> <form name=commaform>Enter a number then click the button: <input type=text name=input size=10 value=""> <input type=button value="Convert" onclick="dollarAmount(this.form.name, 'input')"> <br><br> or enter a number and click another field: <input type=text name=input2 size=10 value="" onBlur="dollarAmount(this.form.name, this.name)"> </form> Hi there, I have scoured the internet looking for the right code but to no avail. I have gotten as far as totaling up my form but now want to format the amounts and total as currency and also want to strip out the values that =0. I have tried to use the code from other solutions (posted on the net) but I just think it is over my head because I cant figure it out. I thought I was close a few times but wasn't able to bring it on home. One of my issues is using somebody's code and not knowing where in my code to place it. http://www.whackyweedremoval.com/wwr-invoice.html I have tried exhaustively to solve on my own but there is a point. Please help I have been working on this code for a few days now and it works. Or at least it appears to be working. I would like to have the tot 1, tot 2, tot 3 return with currency formatting. Is there a way to add something to my code to accomplish this? Thank you in advance for any suggestions. Regards Code: <html> <head> <script type="text/javascript" src="autoCurrency.js"></script> <script type="text/javascript"> function doCalc() { var v1 = document.getElementById('value1').value; var v2 = document.getElementById('value2').value; var v3 = document.getElementById('value3').value; document.getElementById('total1').value = ((v1*5)+(v2*1)) * (1-v3/100); document.getElementById('total2').value = ((v1*10)+(v2*1)) * (1-v3/100); document.getElementById('total3').value = ((v1*15)+(v2*1)) * (1-v3/100); } </script> <style type="text/css"> button { width: 80px; } input { margin: 10px 0; } input[type='text'] { width: 50px; } </style> </head> <body> <div> <input id="value1" type="text" value="0" /> <br /> </div> <div> <input id="value2" type="text" value="0" /> <br /> </div> <div> <input id="value3" type="text" value="0" /> <br /> </div> Tot 1:<input id="total1" type="text" /><br /> Tot 2: <input id="total2" type="text" /><br /> Tot 3: <input id="total3" type="text" /><br /> <button class="long" onclick="doCalc();">Calculate</button> </body> </html> |