Click to See Complete Forum and Search --> : 2 Decimal places


banshee
03-18-2004, 02:12 PM
Hi all

My first post :-)

I am VERY new to javascript but managed to make this little bit of javascript work for me but now I am struggling:

<script language='JavaScript'>
<!--
function getEUTOUSD(price) {
document.write(price * 1.23);
}

// -->
</script>

I then call it in my page with this:

<script type="text/javascript"> getEUTOUSD('25.00');
</script>

As you can probably work out, this works out what the $ ammount would be from an inputted € amount.

The problem I have is that I require it to only show two numbers after the decimal like this 45.33 but in some instances it shows 45.33333333333331

I know I have to do it with math round but whatever I try does not work.

Also, the resulted figure it produces creates a space in front of it so when I put the € symbol in front of the result I get € 45.99 instead of €45.99

Any help would be greatly appreciated, I tend not to ask people to do the work for me and just ask when I have done as much as I can do, so I hope this is OK.

Thanks for your help in advance.

steelersfan88
03-18-2004, 02:40 PM
var result = a number
result = result.toFixed(2) // 2 is numbe rof decimal places

My best guess for your other problem is to not guess. If there is a space, it either s from the character itself or is inserted, which could be taken out using substrings.

banshee
03-18-2004, 02:45 PM
Thanks for your reply but your answer completely confused me, it wasn;t the answer I was expecting, where would I place that to get it to show the correct result?

Thanks again.

steelersfan88
03-18-2004, 03:07 PM
Replace your function with:function getEUTOUSD(price) {
var result = price * 1.23
result = "EURO SIGN"+ result.toFixed(2)
document.write(result);
}Then the euro sign .. not really sure about it (note, PHP tags don't allow for the € character, so it is in text now)

banshee
03-18-2004, 03:53 PM
That worked fine, I was able to get the € sybmol in as follows:

function getEUTOUSD(price) {
var result = price * 1.23
result = "\€"+ result.toFixed(2)
document.write(result);
}

Thanks very much for your help, I really appreciate it.

Thank you.