On Tue, 2 Oct 2007, Riccardo (Jack) Lucchetti wrote:
} else {
/* quarterly, monthly, hourly... */
- double x = date(t, pdinfo->pd, pdinfo->sd0);
- int d = ceil(log10(pdinfo->pd));
- char *p, s[32];
- sprintf(s, "%.*f", d, x);
- p = strchr(s, '.');
- if (p == NULL) {
- p = strchr(s, ',');
- }
- if (p != NULL) {
- sscanf(p + 1, "%d", &ret);
- ret -= 1;
+ double x = date(t, pdinfo->pd, pdinfo->sd0);
+ x -= floor(x);
+ int i, d = ceil(log10(pdinfo->pd));
+ for(i=0; i<d; i++) {
+ x *= 10;
}
+ ret = (int) x;
I agree it would be more elegant to avoid strings, but I'm afraid
this works right only because (double) 10 = 9.9999999999909051.
We return a zero-based index (9 in this case). What one would
like to do is
ret = (int) x - 1;
for exact x. But it's going to screw up if x is not exact.
Allin.