Advertisement
teknoraver

numbers

Oct 23rd, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. /*
  2.  * numbers.c - number formatting library
  3.  * Copyright (C) 2019 Matteo Croce <mcroce@redhat.com>
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #define human(x) decimals(x), rounded(x), suffix(x)
  25. #define H ".*f %s"
  26.  
  27. static double rounded(uint64_t n)
  28. {
  29.     if (n >= 9999500000)
  30.         return n / 1000000000.0;
  31.     if (n >= 9999500)
  32.         return n / 1000000.0;
  33.     if (n > 9999)
  34.         return n / 1000.0;
  35.     return n;
  36. }
  37.  
  38. static int decimals(uint64_t n)
  39. {
  40.     if (n >= 999950000000)
  41.         return 0;
  42.     if (n >= 99995000000)
  43.         return 1;
  44.     if (n >= 9999500000)
  45.         return 2;
  46.     if (n >= 999950000)
  47.         return 0;
  48.     if (n >= 99995000)
  49.         return 1;
  50.     if (n >= 9999500)
  51.         return 2;
  52.     if (n >= 999950)
  53.         return 0;
  54.     if (n >= 99995)
  55.         return 1;
  56.     if (n > 9999)
  57.         return 2;
  58.     return 0;
  59. }
  60.  
  61. static char* suffix(uint64_t n)
  62. {
  63.     if (n >= 9999500000)
  64.         return "G";
  65.     if (n >= 9999500)
  66.         return "M";
  67.     if (n > 9999)
  68.         return "K";
  69.     return "";
  70. }
  71.  
  72. void removeDots(char *s) {
  73.     int i, j, n = strlen(s);
  74.     for (i=j=0; i < n; i++)
  75.         if (s[i] != '.')
  76.             s[j++] = s[i];
  77.     s[j] = 0;
  78. }
  79.  
  80. static char nums[][16] = {
  81.     "9.999",
  82.     "10.000",
  83.     "99.994",
  84.     "99.995",
  85.     "999.949",
  86.     "999.950",
  87.     "9.999.499",
  88.     "9.999.500",
  89.     "99.994.999",
  90.     "99.995.000",
  91.     "999.949.999",
  92.     "999.950.000",
  93.     "9.999.499.999",
  94.     "9.999.500.000",
  95.     "99.994.999.999",
  96.     "99.995.000.000",
  97.     "999.949.999.999",
  98.     "999.950.000.000",
  99. };
  100.  
  101. int main(void)
  102. {
  103.     uint64_t n;
  104.     int i;
  105.  
  106.     for(i = 0; i < sizeof(nums) / sizeof(*nums); i++) {
  107.         printf("%15s => ", nums[i]);
  108.         removeDots(nums[i]);
  109.         n = atol(nums[i]);
  110.         printf("%"H"\n", human(n));
  111.     }
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement