SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    england
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation every three characters, add a space?

    Hi

    I'm not sure how to do this.
    I have a large number, maybe 6 or 7 digits, and I want to format it, but every three characters add a space or comma, to turn
    1000000 into 1 000 000

    I've had a look for a function in the manual but can't find one, so I'm thinking I need a loop or some sort, and use the substr function, but how to go about doing it, I'm not sure!!! Anyone give me any advice?

    Cheers!

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes, there is a built-in function that does this. Take a look at number_format.

    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I think you need to build your own function

    Oh my god I didn't know this number_format ()
    Better as this one:
    PHP Code:
    function fothou$width$num) {
       
    $out str_pad(''$width' ');
       
    $col $width 1;
       while (
    $num) {
          
    $out{$col--} = $digit $num 10;
          
    $num-= 10 $digit// no integer division in PHP
          
    $cnt++;
          if (!(
    $cnt 3)) {
             
    $col--;
          }
          if (
    $col 0) {
             die(
    'Overflow');
          }
       }
       return 
    $out;

    It's bloody complicated for a little thing
    Thanx lilleman
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    england
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah! perfect!!! I can't believe I missed it while looking though the manual! But thanks for pointing it out!


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
  •