-
Notifications
You must be signed in to change notification settings - Fork 0
/
krmath.c
70 lines (57 loc) · 1.31 KB
/
krmath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "php.h"
#include "php_krmath.h"
#include "php_krparse.h"
#include <math.h>
#include <float.h>
char * kr_math_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 (1 + DBL_MAX_10_EXP + 1 + dec + 1);
tmplen = sprintf (tmpbuf, "%.*f", dec, d);
if ( ! isdigit ((int) tmpbuf[0]) )
return tmpbuf;
if ( dec ) {
reslen = dec + 1 + (tmplen-dec - 1) + ((thousand_sep) ? (tmplen - 1 - dec - 1) / 3 : 0);
} else {
reslen = tmplen + ((thousand_sep) ? (tmplen - 1) / 3 : 0);
}
if ( is_negative )
reslen++;
resbuf = (char *) emalloc (reslen + 1);
s = tmpbuf + tmplen - 1;
t = resbuf + reslen;
*t-- = 0;
if ( dec ) {
while ( isdigit ((int) *s) )
*t-- = *s--;
*t-- = dec_point; /* copy that dot */
s--;
}
while ( s >= tmpbuf ) {
*t-- = *s--;
if ( thousand_sep && (++count%3) == 0 && s >= tmpbuf ) {
*t-- = thousand_sep;
}
}
if ( is_negative )
*t-- = '-';
kr_safe_efree (tmpbuf);
return resbuf;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/