SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting Number

    Hi All,

    I want to take only first 2 digit (not round up!) and format my number.
    .toFixed and .toPrecision not applicable for my request.

    myNumber = 1234.567

    I want to show this number like this:
    1,234.56

    alert(myNumber * 2) shoud display:
    2469.12 (NOT 2469,134)

    Help please....

  2. #2
    doRighteousDeeds++
    Join Date
    Aug 2006
    Location
    تركيا Turkey Türkiye
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    
    var str = "1234.567".replace(/(\d+)(?=\d{3})/g, '$1,');
    
    alert(str);
    
    alert(str.substring(0,str.length-1));
    
    </script>
    Code:
      
    <script type="text/javascript">
    
    var str = "1234.567".split('').reverse().join('').replace(/(\d{3})(?=\d)/g, '$1,').split('').reverse().join('');
    
    alert(str);
    
    alert(str.substring(0,str.length-1));
    
    </script>
    Last edited by muazzez; Apr 30, 2007 at 05:59.

  3. #3
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    828
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by muazzez View Post
    Code:
      
    <script type="text/javascript">
    
    var str = "1234.567".replace(/(\d+)(?=\d{3})/g, '$1,');
    
    alert(str);
    
    alert(str.substring(0,str.length-1));
    
    </script>
    Superb....
    Thanks


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
  •