[PHP-DEV] CVS update: php3/functions From: zeev (php-dev <email protected>)
Date: 04/29/98

Date: Wednesday April 29, 1998 @ 19:35
Author: zeev

Update of /repository/php3/functions
In directory asf:/tmp/cvs-serv19144/functions

Modified Files:
        math.c
Log Message:
- Fixed number_format() to properly handle negative numbers, and optimized it
  a bit.

Index: php3/functions/math.c
diff -c php3/functions/math.c:1.32 php3/functions/math.c:1.33
*** php3/functions/math.c:1.32 Wed Apr 29 09:24:17 1998
--- php3/functions/math.c Wed Apr 29 19:35:57 1998
***************
*** 465,480 ****
  char *_php3_number_format(double d,int dec,char dec_point,char thousand_sep)
  {
          char *tmpbuf,*resbuf;
- char format[16];
          char *s,*t; /* source, target */
          int tmplen,reslen=0;
          int count=0;
          
          dec = MAX(0,dec);
          tmpbuf = (char *) emalloc(32+dec);
          
! sprintf(format,"%%.%df",dec);
! tmplen=_php3_sprintf(tmpbuf,format,d);
  
          for (t=tmpbuf; *t; t++) {
                  if (*t=='.') {
--- 465,483 ----
  char *_php3_number_format(double d,int dec,char dec_point,char thousand_sep)
  {
          char *tmpbuf,*resbuf;
          char *s,*t; /* source, target */
          int tmplen,reslen=0;
          int count=0;
+ int is_negative=0;
          
+ if (d<0) {
+ is_negative=1;
+ d = -d;
+ }
          dec = MAX(0,dec);
          tmpbuf = (char *) emalloc(32+dec);
          
! tmplen=_php3_sprintf(tmpbuf,"%.*f",dec,d);
  
          for (t=tmpbuf; *t; t++) {
                  if (*t=='.') {
***************
*** 486,491 ****
--- 489,497 ----
          } else {
                  reslen = tmplen+(tmplen-1)/3;
          }
+ if (is_negative) {
+ reslen++;
+ }
          resbuf = (char *) emalloc(reslen+1);
          
          s = tmpbuf+tmplen-1;
***************
*** 504,509 ****
--- 510,518 ----
                  if ((++count%3)==0 && s>=tmpbuf) {
                          *t-- = thousand_sep;
                  }
+ }
+ if (is_negative) {
+ *t-- = '-';
          }
          efree(tmpbuf);
          return resbuf;