PDA

View Full Version : How do i display pound sign in Javascript.



eesyboi
02-07-2012, 12:56 PM
I am building an order form that calculates total on select.... But the problem am facing with the javascript i've got is that it displays $ sign but i would like it to display sign instead... Here is what the code look like. I would be grateful if someone can show me or help me in changing this.



function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = "0" + pence;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + pence);
}

vwphillips
02-07-2012, 01:39 PM
return (((sign)?'':'-') + '' + num + '.' + pence);

eesyboi
02-07-2012, 03:36 PM
Thank you for your reply, however i tried changing that already, but when i do, the result doesn't display properly.. Please see the attached image for the result.

http://twitpic.com/8gwmju

vwphillips
02-07-2012, 03:49 PM
both display the sign correctly for me


<!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" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
pence = num%100;
num = Math.floor(num/100).toString();
if(pence<10)
pence = "0" + pence;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '' + num + '.' + pence);
}/*]]>*/
</script></head>

<body>

<span id="s" ></span>
<input id="ip" name="" />

<script>
document.getElementById('s').innerHTML=formatCurrency(100.5);
document.getElementById('ip').value=formatCurrency(100.5);
</script>

</body>

</html>

jscheuer1
02-07-2012, 04:50 PM
From your image, it looks like an encoding issue. If I had to guess I'd say that the script is encoded as UTF-8 and that the page is not. Perhaps the page is encoded as ISO-8859-1 or windows-1252.

To tell for sure and to determine a good solution we would need a link to the page.

If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.

eesyboi
02-07-2012, 06:46 PM
From your image, it looks like an encoding issue. If I had to guess I'd say that the script is encoded as UTF-8 and that the page is not. Perhaps the page is encoded as ISO-8859-1 or windows-1252.

To tell for sure and to determine a good solution we would need a link to the page.

If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.

Yes!!!!!!! :) it is the encoded thing.. I changed it to UTF-8 in my page header and it worked but i was thinking, if its possible to have two charset and add the encoding UTF-8 to my page and leave what i had there before which is


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

can i do something like


<meta http-equiv="Content-Type" content="text/html; charset='ISO-8859-1,UTF-8';" />


Or would my page work fine with UTF-8?

jscheuer1
02-07-2012, 08:32 PM
You can't have two encodings on the same page, no. But almost everything (on your average English language page) that looks OK in ISO-8859-1 will look OK in utf-8. For those few things that don't (if there are any on your page), you can always save your page in utf-8, then they will be fine as well.

Most editors allow you to change from one encoding to another. If yours doesn't, you can use NotePad - load the file in NotePad, choose 'save as', note the encoding dropdown that appears near the bottom right of the Save As dialog box, choose utf-8 and save.

eesyboi
02-07-2012, 08:41 PM
You can't have two encodings on the same page, no. But almost everything (on your average English language page) that looks OK in ISO-8859-1 will look OK in utf-8. For those few things that don't (if there are any on your page), you can always save your page in utf-8, then they will be fine as well.

Most editors allow you to change from one encoding to another. If yours doesn't, you can use NotePad - load the file in NotePad, choose 'save as', note the encoding dropdown that appears near the bottom right of the Save As dialog box, choose utf-8 and save.
Thanks... I appreciate the explanation.