summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdin Kadribasic <edink@php.net>2003-01-16 13:21:54 +0000
committerEdin Kadribasic <edink@php.net>2003-01-16 13:21:54 +0000
commitf7b9d75e18bc4e8897342bdd8d6231489baeca37 (patch)
treee0f26788319020bf8976ff1362d57f92ae19de99
parent5295f6e657ecece0fc6a2c8e73bf98c5bdc47655 (diff)
downloadphp-git-f7b9d75e18bc4e8897342bdd8d6231489baeca37.tar.gz
MFH: recent fixes to number_format()
-rw-r--r--ext/standard/math.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/ext/standard/math.c b/ext/standard/math.c
index e6bf03c2c2..137b228d96 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -1011,10 +1011,14 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho
/* allow for thousand separators */
if (thousand_sep) {
- integral += integral / 3;
+ integral += (integral-1) / 3;
}
- reslen = integral + 1 + dec;
+ reslen = integral;
+
+ if (dec) {
+ reslen += 1 + dec;
+ }
/* add a byte for minus sign */
if (is_negative) {
@@ -1034,21 +1038,24 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho
int topad = declen > 0 ? dec - declen : 0;
/* pad with '0's */
+
while (topad--) {
*t-- = '0';
}
-
- /* now copy the chars after the point */
- memcpy(t - declen + 1, dp + 1, declen);
- t -= declen;
- s -= declen;
+ if (dp) {
+ /* now copy the chars after the point */
+ memcpy(t - declen + 1, dp + 1, declen);
+
+ t -= declen;
+ s -= declen;
+ }
/* add decimal point */
*t-- = dec_point;
s--;
}
-
+
/* copy the numbers before the decimal place, adding thousand
* separator every three digits */
while(s >= tmpbuf) {
@@ -1064,7 +1071,7 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho
}
efree(tmpbuf);
-
+
return resbuf;
}