SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Format of float numbers

    I sum up some values with jQuery, which automatically inserts a dot if the result is a float. However, I would like to use a comma instead, how can I do this?

  2. #2
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this can be of any help, but I forgot to say that I use this function to add a dot every 3 numbers to improve readability:

    Code:
    function aggiungiPunti(x) {
       	return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ".");
    }

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by D3V4 View Post
    I sum up some values with jQuery, which automatically inserts a dot if the result is a float. However, I would like to use a comma instead, how can I do this?
    You can convert the number to a string, and replace fullstops with commas.

    Code javascript:
    var value = '12.3'.toString().replace('.', ',');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    
    <script type="text/javascript">
    
    var m = 12345678912354.23
    var mm= String(m).split('.');
    // alert(mm);
    var n = mm[0];
    // var n = 12345678912354;
    var s = String(n).length;
    
    var d = ( s % 3 == 0 ) ? (s/3) : Math.floor(s/3) + 1;
    
    // alert(d);
    
    var A =[];
    
    A.length= d;
    n= Number( n );
    for(var i = 1; i<= d; i++) {
    
    A[A.length-i] =  n % 1000;
    
    n =  parseInt(  n/1000);
    
    // alert(n);
    
    // alert(A);
    
    }  
    
    var res = A.join('.');  // 12.345.678.912.354; 
    
    //alert(res);
    
    var h = res + "," + mm[1];
    
    alert(h); // 12.345.678.912.354,23
    
    
    </script>
    The Time Through Ages
    1. By the Time, 2. Verily Man is in loss, 3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    You can convert the number to a string, and replace fullstops with commas.

    Code javascript:
    var value = '12.3'.toString().replace('.', ',');
    Quote Originally Posted by muazzez View Post
    Code:
      
    
    <script type="text/javascript">
    
    var m = 12345678912354.23
    var mm= String(m).split('.');
    // alert(mm);
    var n = mm[0];
    // var n = 12345678912354;
    var s = String(n).length;
    
    var d = ( s % 3 == 0 ) ? (s/3) : Math.floor(s/3) + 1;
    
    // alert(d);
    
    var A =[];
    
    A.length= d;
    n= Number( n );
    for(var i = 1; i<= d; i++) {
    
    A[A.length-i] =  n % 1000;
    
    n =  parseInt(  n/1000);
    
    // alert(n);
    
    // alert(A);
    
    }  
    
    var res = A.join('.');  // 12.345.678.912.354; 
    
    //alert(res);
    
    var h = res + "," + mm[1];
    
    alert(h); // 12.345.678.912.354,23
    
    
    </script>
    Thank you guys!


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
  •