From: Jakob Haufe Date: Thu, 5 Oct 2017 22:47:34 +0000 (+0200) Subject: Rework format_euro() X-Git-Tag: rgb2rv17~13^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=81899e9ef7a6c5562a0f4e2dd7eacee03cab3507;p=c128-kasse Rework format_euro() Should have been this way from the start. As of today, it's unclear whether there has been a real bug in the library back then of just a confusion between %02d and %0.2d. An simple benchmark of 1000 iterations resulted in approximately 25% speedup at 1MHz and 12.5% speedup at 2MHz. --- diff --git a/src/general.c b/src/general.c index 668bdcf..ad00497 100644 --- a/src/general.c +++ b/src/general.c @@ -75,15 +75,8 @@ char retry_or_quit(void) { } char *format_euro(char *s, int maxlen, int cent) { - int tmp = cent; - int len = strlen(",EUR"); - while ((tmp /= 10) > 0) - ++len; - if (len >= maxlen) + if(snprintf(s,maxlen,"%3d,%02dEUR", cent/100, cent%100) > maxlen) return NULL; - // workaround to produce a leading zero for cents.. %0.2d won't work - sprintf(s, "%3d,%s%dEUR", cent / 100, ((cent % 100) < 10 ? "0" : ""), - cent % 100); return s; }