From 81899e9ef7a6c5562a0f4e2dd7eacee03cab3507 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Fri, 6 Oct 2017 00:47:34 +0200 Subject: [PATCH 1/1] 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. --- src/general.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) 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; } -- 2.39.2