fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. char toNotationUnit(double value,float *out) {
  4. double val;
  5. char submultiplo[] = {' ','m','u','n','p','f','a','z','y'};
  6. char multiplo[] = {' ','k','M','G','T','P','E','Z','Y'};
  7. int counter;
  8. char unit;
  9. val = value;
  10. if(val < 1) {
  11. while(val < 1.00) {
  12. counter++;
  13. val=val*(double)1000;
  14. if(counter==8) break;
  15. unit = submultiplo[counter];
  16. }
  17. }else{
  18. while(val >= 1000) {
  19. counter++;
  20. val=val/(double)1000;
  21. if(counter==8) break;
  22. }
  23. unit = multiplo[counter];
  24. }
  25. val = round(val*(double)100)/(double)100;
  26. *out = (float)val;
  27. return unit;
  28. }
  29.  
  30. int main() {
  31. double x = 0.000000123123;
  32. float res;
  33. char t;
  34. t = toNotationUnit(x,&res);
  35. printf("%.2F%c",res,t);
  36. return 0;
  37. }
stdin
Standard input is empty
compilation info
prog.c: In function ‘toNotationUnit’:
prog.c:25: warning: implicit declaration of function ‘round’
prog.c:25: warning: incompatible implicit declaration of built-in function ‘round’
prog.c:3: warning: ‘counter’ may be used uninitialized in this function
prog.c:8: warning: ‘unit’ may be used uninitialized in this function
stdout
123.12n