I want to round a float/double number to n decimal places. But not in printing, but in general.
e.g: User's input is: 2.3891 3, so the output should be 2.389
or if the user's input 5.98441 4, the output should be: 5.9844
#include <stdio.h>
#include <math.h>
int main () {
float num;
int decimals;
scanf("%f%d", &num, &decimals); //5.2381 2
int res = num; // res = 5
int power = 1;
int i = 0;
for(i = 0; i < decimals; i++)
power*=10; // 1 * 10 * 10
int num3 = num * power; // num3 = 523
int num2 = num3 % power;//
float a = num2*1.0 / power*1.0;
// float a = 23*1.0/13.0;
printf("%f %d\n", a, power);
a = res + a;
printf("%f", a);
return 0;
}
As the output here i get: 2.390000. I don't want to have these zeros and i don't want to use fixed %.2f. What am I missing?