Results 1 to 8 of 8

Thread: How do i display pound sign in Javascript.

  1. #1
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How do i display pound sign in Javascript.

    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.

    Code:
    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);
    }

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
      return (((sign)?'':'-') + '£' + num + '.' + pence);
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by jscheuer1; 02-07-2012 at 04:43 PM. Reason: Format

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    both display the £ sign correctly for me

    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" 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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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

    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    can i do something like

    Code:
    <meta http-equiv="Content-Type" content="text/html; charset='ISO-8859-1,UTF-8';" />
    Or would my page work fine with UTF-8?
    Last edited by eesyboi; 02-07-2012 at 08:08 PM.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    eesyboi (02-07-2012)

  9. #8
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •