How to round a number in c?
source link: https://www.codeabbey.com/index/forum_topic/876633e4b6558861b6848e62e52d10b8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to round a number in c? |
Back to General discussions forum
include <stdio.h>
define MAX 23
int main (){ long aListA[MAX] = {2141850,6315308,8021447,62,150,105,7429,19769,63,5673,-3005588,5097005,8379066,8290999,162,10375,8125699,6629167,105,16193,-6523290,164,5266988 }; long aListB[MAX] = {4421758,126,2253209,4,12,6,1888,1928,6,864,-33190,376,-3066051,186,12,1774,679,355,14,1548,2263824,8,205}; long aListC[MAX]; long eMod, i = 0; double fAux;
for(i = 0; i < MAX; i++){
aListC[i] = aListA[i] / aListB[i];
// printf("%ld\n", aListC[i]);
fAux = (double) (aListA[i]) / aListB[i];
// printf("%f\n", fAux);
if(aListC[i] < 0){
// fAux = (double) (aListA[i]) / aListB[i];
// printf("%f\n", fAux);
fAux = fAux + aListC[i];
// printf("%f\n", fAux);
if(fAux <= -0.5){
aListC[i] = aListC[i] - 1;
// printf("%ld\n", aListC[i]);
}
} else {
// fAux = (float) (aListA[i]) / aListB[i];
// printf("%f", fAux);
fAux = fAux - aListC[i];
if(fAux >= 0.5){
aListC[i] = aListC[i] + 1;
// printf("%ld\n", aListC[i]);
}
}
printf("%ld\n", aListC[i]);
}
getchar();
return 0;
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK