round.c 372 B

123456789101112131415161718
  1. #ifdef PRINTF_STDLIB
  2. #include <stdio.h>
  3. #endif
  4. #ifdef PRINTF_CUSTOM
  5. #include "tinystdio.h"
  6. #endif
  7. float roundto(float value, unsigned char digits) {
  8. unsigned long int pw = 10;
  9. for (unsigned char i = 0; i < digits; i++)
  10. pw = pw * 10;
  11. if (value == 0)
  12. return 0;
  13. value = value >= 0 ? value + (5.0/pw) : value - (5.0/pw);
  14. return value;
  15. }