Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Currency info

thehen

Coder
I have a webpage ,that gets and show currency in different currency info.
Then I have html code with a box that I manually type in the currency I want to use.
Then I have JavaScript to add two numbers together.

How can I use JavaScript or html to get the correct currency I want and add it to the the box by itself, and add it with other number.

[CODE lang="html" title="Html"] <label for="usd">leave blank</label>
<input readonly="readonly" type="usdzar" id="usd" name="usd">
<label for="zar"></label>
<input style="color: #17202A; font-family: Comic Sans MS; font-weight: bold; font-size: 12px; background-color: #AED6F1; type="usdzar" id="zar" name="zar">
<button type="button" onclick="multiply1()">Multiply</button>
<input style="color: #17202A; font-family: Comic Sans MS; font-weight: bold; font-size: 12px; background-color: #E8F8F5; type="text" id="result1" name="result1">
[/CODE]

JavaScript:
 <script>
        function multiply1(){
            var num3 = document.getElementById("result").value.replace(/,/g,''); //remove any commas
            var num4 = document.getElementById("zar").value;
            var result1 = num3 * num4;
            document.getElementById("result1").value = result1.toLocaleString('en-US',{ maximumFractionDigits: 8 });
            document.getElementById("result1").style.color = "red";
            document.getElementById("result1").style.fontWeight = "900";
        }
        </script>
 
Your code is all over the place and does not reflect what you state in the text. But here's what I think is wrong with the code and a means to correct it.
You have 3 <input> boxes, the first to use type="usdzar" There is no such thing. Here is a list of input types:
  • <input type="button">
  • <input type="checkbox">
  • <input type="color">
  • <input type="date">
  • <input type="datetime-local">
  • <input type="email">
  • <input type="file">
  • <input type="hidden">
  • <input type="image">
  • <input type="month">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="range">
  • <input type="reset">
  • <input type="search">
  • <input type="submit">
  • <input type="tel">
  • <input type="text">
  • <input type="time">
  • <input type="url">
  • <input type="week">
Nothing special for currency. I think you're trying to use U.S. Dollars and South African Rand. Also note that info entered into input boxes are almost always treated as a string.
----------------------------------------------
Your first line in your JS fuction has .......getElementById("result") It should be getElementById("result1"). Notice the 1.
You can convert a string to a number by doing this
var result1 = Number(num3) * Number(num4);

You show the results by this:
document.getElementById("result1").innerHTML= result1;

You used tolocalestring which is a conversion of a Date object to a string.


Hope this gets you started on the right path. If your still having problem come back and post your new code.
 
Your code is all over the place and does not reflect what you state in the text. But here's what I think is wrong with the code and a means to correct it.
You have 3 <input> boxes, the first to use type="usdzar" There is no such thing. Here is a list of input types:
  • <input type="button">
  • <input type="checkbox">
  • <input type="color">
  • <input type="date">
  • <input type="datetime-local">
  • <input type="email">
  • <input type="file">
  • <input type="hidden">
  • <input type="image">
  • <input type="month">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="range">
  • <input type="reset">
  • <input type="search">
  • <input type="submit">
  • <input type="tel">
  • <input type="text">
  • <input type="time">
  • <input type="url">
  • <input type="week">
Nothing special for currency. I think you're trying to use U.S. Dollars and South African Rand. Also note that info entered into input boxes are almost always treated as a string.
----------------------------------------------
Your first line in your JS fuction has .......getElementById("result") It should be getElementById("result1"). Notice the 1.
You can convert a string to a number by doing this
var result1 = Number(num3) * Number(num4);

You show the results by this:
document.getElementById("result1").innerHTML= result1;

You used tolocalestring which is a conversion of a Date object to a string.


Hope this gets you started on the right path. If your still having problem come back and post your new code.
Thank you
The ("result1") is correct, getting it from a other part of my code, and the input of usdzar, got it from the internet.
 
Did you get things working?
No, still tying to find out how to use the code you gave me,
document.getElementById("result1").innerHTML= result1;
Still try to figure out how it will get the correct currency, I am not very good at this at the moment with JavaScript, still learning, but it is going slow.
I can give you the webpage info, if you want to see what I am trying to do.
 
Ok thank you

1. first row, second column ( add amount in eg 1.00 or 0.01.
2. press button om first row to get total.
3. second row, column 4 (this is where I want to auto add ZAR from the currency showing), press button in row to get total
4. the result for column 1 in row two is coming from the last column in row 1

Hope you understand my logic
 

Attachments

  • TEST.TXT
    12.3 KB · Views: 2
I can not figure out what your trying to do.
1. first row, second column ( add amount in eg 1.00 or 0.01.
What does that even mean??????

-----

In easy to understand words what are you trying to do. What does "COIN VALUE NOW IN USD" Mean??? The only COIN you have are in column 3, is that what your thinking of?

CONVERT TO ZAR
Convert what?

And most importantly Why multiply if your looking for a total?
 
I know now I must use this for num4, but do not know which to use

[CODE title="javascript"]<script>function reloadFnAj0a2vV(){ var sc = document.getElementById('scFnAj0a2vV');if (sc) sc.parentNode.removeChild(sc);sc = document.createElement('script');sc.type = 'text/javascript';sc.charset = 'UTF-8';sc.async = true;sc.id='scFnAj0a2vV';sc.src = 'https://freecurrencyrates.com/en/widget-horizontal?iso=USD-ZAR&df=1&p=FnAj0a2vV&v=fits&source=fcr&width=500&width_title=225&firstrowvalue=1&thm=8E846B,FEEEBD,494437,817865,FFFFFF,D19405,FECE2F,383838,000000&title=Currency%20Converter&tzo=-120';var div = document.getElementById('gcw_mainFnAj0a2vV');div.parentNode.insertBefore(sc, div);} reloadFnAj0a2vV(); </script>[/CODE]
 
I can not figure out what your trying to do.

What does that even mean??????

-----

In easy to understand words what are you trying to do. What does "COIN VALUE NOW IN USD" Mean??? The only COIN you have are in column 3, is that what your thinking of?


Convert what?

And most importantly Why multiply if your looking for a total?
Fi
I can not figure out what your trying to do.
What does that even mean??????
That is just a example of what I use.

Convert from USD to ZAR

I want to get a total of the value I put in * the curency (say today it is ZAR 14.00 to 1USD) so if I have 2.0$ it must give me 14*2
 
The following JavaScript code will calculate a currency exchange between a starting and ending currency. It will provide the exchange rate and the final amount.
I published this code to Github, so I really really hope that it can benefit anyone trying to program a currency exchange rate calculator with JavaScript. The cool thing is that it can be used for other languages too so coding this logic for other systems is fairly easy to do.
JavaScript:
var start_currency = "USD", //  usd, gbp, eur, btc, etc...
    end_currency =  "EUR", // usd, gbp, eur, btc, etc...
    start_amount = "100", // change this to anything you want
    currencies = {"USD":{"EUR":0.85, "GBP":0.73, "BTC":0.000032}, "EUR":{"USD":1.18, "GBP":0.86, "BTC":0.000038}, "BTC":{"EUR":26542.01, "GBP":22747.76, "BTC":31333.90}} // currency exchange rates must be updated frequently, with use of API or another database
if(typeof currencies[start_currency] != 'undefined' && typeof currencies[end_currency] != 'undefined'){
    var exchange_rate = currencies[start_currency][end_currency],
          end_amount = exchange_rate * start_amount;
    console.log("1 " + start_currency + " is equal to " + end_result + " " + end_currency + " so  " + start_amount + " " +  start_currency + " is equal to " + end_amount + " " + end_currency)
} else {
    console.log("This currency is not supported at this time.")
}

How you get the exchange rates is a whole other challenge. Do you connect to an API or anything?
The code I just wrote above will at least show you the general logic behind taking a starting currency, an end currency, and calculating the exchange rate, as well as what the new amount is once exchanged so you can actually see it in use. You can open developer tools in your browser and use the Console log to play with this JS, or put it on a site or use the Codeforum IDE...

I hope this helps :)
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom