fork(3) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int pow_i(int base, int exp){
  5. int result = 1;
  6. while (exp){
  7. if (exp & 1)
  8. result *= base;
  9. exp >>= 1;
  10. base *= base;
  11. }
  12. return result;
  13. }
  14.  
  15. float round_float_precision(float x, unsigned digits){
  16. unsigned precision = pow_i(10, digits);
  17. return roundf(x*precision)/precision;
  18. }
  19.  
  20. int main(void) {
  21. float x = 123.456;
  22. printf("Two digits of precision\n");
  23. printf("- by printf - %.2f\n", x);
  24. printf("- by setting value precision - %f\n", round_float_precision(x, 2));
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
Two digits of precision
- by printf                  - 123.46
- by setting value precision - 123.460000