Author Topic: please help  (Read 1610 times)

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
please help
« on: February 11, 2008, 08:45:22 PM »
please help me with my (,) issue.
my numbers show up as 1000 or 100000.
i want them to have the commas so it be like this 1,000 or 100,000.

this is the code i have.
Quote
function prettynum($num,$dollar="0") {
// Basic send a number or string to this and it will add commas. If you want a dollar sign added to the
// front and it is a number add a 1 for the 2nd variable.
// Example prettynum(123452838,1)  will return $123,452,838 take out the ,1 and it looses the dollar sign.
        $out=strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $num ) ) );
      if ($dollar && is_numeric($num)){
         $out= "$".$out;
      }
   return $out;
}

Offline FirePenguins

  • Level 10
  • *
  • Posts: 59
  • Reputation: +0/-0
    • View Profile
    • The Iron Helm
Re: please help
« Reply #1 on: February 11, 2008, 09:04:26 PM »
just use

number_format($number)  http://us3.php.net/number_format

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
Re: please help
« Reply #2 on: February 11, 2008, 09:32:59 PM »
i want to use that 1 though?
what would i have to change in my code to add commas?

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: please help
« Reply #3 on: February 12, 2008, 02:50:39 AM »
Change this line: $out= "$".$out;
To: $out= "$".number_format($out);

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
Re: please help
« Reply #4 on: February 12, 2008, 04:33:11 PM »
Quote
function prettynum($num,$dollar="0") {
// Basic send a number or string to this and it will add commas. If you want a dollar sign added to the
// front and it is a number add a 1 for the 2nd variable.
// Example prettynum(123452838,1)  will return $123,452,838 take out the ,1 and it looses the dollar sign.
        $out=strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $num ) ) );
      if ($dollar && is_numeric($num)){
         $out= "$".number_format($out);
      }
   return $out;
}

that what i have now, still doesnt work.

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: please help
« Reply #5 on: February 13, 2008, 12:18:29 AM »
how do you call the function ?

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
Re: please help
« Reply #6 on: February 13, 2008, 01:31:56 AM »
Quote
$this->money = $worked['money'];<--- in classes.php

then   in header.php ------> $out = str_replace("<!_-money-_!>", $user_class->money, $out);

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: please help
« Reply #7 on: February 13, 2008, 01:59:06 AM »
err.. as i can see you never do a call to your prettynum function....

I don't know how the script is done, but nonetheless i think it should be like that (i don't see the point for a self reference to $out by the way... unless there is something assigned to that variable before) :

Code: [Select]
$out = str_replace("<!_-money-_!>", prettynum($user_class->money,1), $out);

Offline PROMO

  • Level 6
  • *
  • Posts: 22
  • Reputation: +0/-0
    • View Profile
Re: please help
« Reply #8 on: February 13, 2008, 05:29:44 AM »
Quote
$out = str_replace("<!_-money-_!>", prettynum($user_class->money), $out);

that what i put.
OMFG!!! leZourite you are good!
thank you for helping me. I FINALLY GOT IT! YES.

ok on to the next problem. :D

Offline thevtm

  • Level 4
  • *
  • Posts: 12
  • Reputation: +0/-0
    • View Profile
Re: please help
« Reply #9 on: April 19, 2008, 03:06:51 PM »

Offline leZourite

  • Level 12
  • *
  • Posts: 81
  • Reputation: +2/-0
    • View Profile
    • After doomsday
Re: please help
« Reply #10 on: April 19, 2008, 04:31:32 PM »
he wanted the numbers to be formated, round doesn't do that (not that way)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal