Implementa funções para obter reais e centavos.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 21:24:35 +0000 (18:24 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sat, 1 Aug 2015 21:24:35 +0000 (18:24 -0300)
base.c
util.h

diff --git a/base.c b/base.c
index 621ad08..b4bf17d 100644 (file)
--- a/base.c
+++ b/base.c
@@ -41,13 +41,17 @@ static int run_resumo(struct declaracao *dec, char **args, int argc)
                printf("retificadora\n");
        if (dec->obrigatoria)
                printf("obrigatoria\n");
-       printf("pago: %lld.%02d\n", dec->pago / 100, dec->pago % 100);
-       printf("retido: %lld.%02d\n", dec->retido / 100, dec->retido % 100);
-       printf("devido: %lld.%02d\n", dec->devido / 100, dec->devido % 100);
+       printf("pago: %lld.%02d\n", reais(dec->pago), centavos(dec->pago));
+       printf("retido: %lld.%02d\n", reais(dec->retido),
+                                     centavos(dec->retido));
+       printf("devido: %lld.%02d\n", reais(dec->devido),
+                                     centavos(dec->devido));
        if (dec->restituicao > 0)
-               printf("restituicao: %lld.%02d\n", dec->restituicao / 100, dec->restituicao % 100);
+               printf("restituicao: %lld.%02d\n", reais(dec->restituicao),
+                                                  centavos(dec->restituicao));
        if (dec->pagar > 0)
-               printf("a pagar: %lld.%02d\n", dec->pagar / 100, dec->pagar % 100);
+               printf("a pagar: %lld.%02d\n", reais(dec->pagar),
+                                              centavos(dec->pagar));
        return 0;
 }
 
diff --git a/util.h b/util.h
index 72b66a0..758d67b 100644 (file)
--- a/util.h
+++ b/util.h
@@ -57,4 +57,18 @@ static struct cmd cmd_##suffix = { \
 
 #define SET_STRING(attr) SET_STRING_(attr, attr, attr)
 
+static inline long long reais(long long val)
+{
+       return val / 100LL;
+}
+
+/* Sempre retorna um valor positivo. */
+static inline int centavos(long long val)
+{
+       if (val > 0LL)
+               return (int) (val % 100LL);
+       else
+               return (int) (-val % 100LL);
+}
+
 #endif