fork(4) download
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <math.h>
  4.  
  5. long double round_f(long double number, int decimal_places)
  6. {
  7. assert(decimal_places > 0);
  8.  
  9. double power = pow(10, decimal_places-1);
  10. number *= power;
  11.  
  12. return (number >= 0) ? ((int)(number + 0.5))/power : ((int)(number - 0.5))/power;
  13. }
  14.  
  15. int main(void)
  16. {
  17. printf("%Lf\n", round_f(1.24, 2));
  18. printf("%Lf\n", round_f(1.25, 2));
  19.  
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
1.200000
1.300000