0

hi I actually trying to do calculate the two numbers,, it was working, but now I don't know how to turn the number into currency. so before the number it need to show £ sign. I was researched in Google ,and i tried some things but its not actually working.

var name,child,result;

function setvalues()
{
   name=Number(document.getElementById("name").value);
   child=Number(document.getElementById("child").value);
}

on that name field user has to enter some number to calculate,, its was calculating but just a number not with an currency sign... I seen some of the JavaScript function to change the currency format but its not very clear... and how to add three vaules together and show in one textbox with £ sign

  • 1
    The HTML entity is £ – mvw Mar 20 '14 at 20:49
  • It seems your problem is not calculating the numerical value for the currency, but displaying a well formatted string representation of some currency value. – mvw Mar 20 '14 at 20:53
  • var name,child,result; function setvalues() { name=Number(document.getElementById("name").value); child=Number(document.getElementById("child").value); } function sum() { setvalues(); result= name*child; document.getElementById('total').value=result; } this is actually my code its working fine.. but i am trying to change into currency using javascript – user3443845 Mar 20 '14 at 20:54
  • 1
    Use something like: `var total = '£' + result.toFixed(2);` – RobG Mar 20 '14 at 21:09
  • where you want me to put that code rob? – user3443845 Mar 20 '14 at 21:37

1 Answers1

0

I am unsure of what you are trying to accomplish but I will try to answer as best I can. I would recommend that if you are working with currency(which can be a especially be a pain in JavaScript). I would recommend using a library to handle this for you. I have used accounting.js in the past and it works very well for these sorts of things:

http://josscrowcroft.github.io/accounting.js/

If you update your question with more information I will update my answer.

UPDATE

Try this: http://codepen.io/anon/pen/BjIHn/. It should produces the output you are wanting.

Stratus3D
  • 4,648
  • 4
  • 35
  • 67
  • i was using javascript – user3443845 Mar 20 '14 at 21:01
  • sorry i got a probelm now ... it is showing the £ sign but now its showing 0 after the pound sign with the price and other 2 zero.. – user3443845 Mar 20 '14 at 21:30
  • Look at the JavaScript code in the box on the top right. I added the pound sign to the beginning of the string that is set as the value of the input. If my answer solves your problem please mark it as accepted. – Stratus3D Mar 20 '14 at 21:30
  • What did you provide as input? The result may need to be rounded. See this answer for more details: http://stackoverflow.com/questions/9453421/how-to-round-float-numbers-in-javascript – Stratus3D Mar 20 '14 at 21:34
  • actually it was working showing as a whole number,, but i was trying to add other value... like an third ..input box.. to caculate the total amount plus the third box number,, but its not actually showing the correct vaule... i think i know what is doing.. its just adding numbers like an £ sign .. but its not caculating the price when i add the 3 value – user3443845 Mar 20 '14 at 21:50
  • Yes. '1' + '2' (strings) will result in '12', 1 + 2 (integers) will result in 3. You will need to convert the value of the input field to a number before performing the arithmetic. – Stratus3D Mar 21 '14 at 12:42