- 1
How to convert an float into a whole number for example 5.5 to 6 and 5.4 to 5
9 ответов
+ 1
try using Math.Round(5.5);
+ 1
For this you may use round() function.
I am telling according to my java experience
I don't know if the same function works but I hope so it may help you
+ 1
If you are not satisfied then see the link
https://fresh2refresh.com/c-programming/c-arithmetic-functions/c-round-function/
+ 1
If you're doing this in C language (tagged language), then you need to include <math.h> header, and use round() function.
Since you want the final result as whole number, don't forget to cast the value returned from round() function.
0
Ok i will try
0
Ans is not crt
0
Do u know any other relevant ans
0
If your just want to display output as a rounded float just use the formating options in printf.
#include <stdio.h>
int main(void) {
float a = 4.8;
float b = 5.2;
printf("%2.0f\n%2.0f", a, b);
}
Out put is:
5
5
however if you need an int variable that has been rounded from floats use round as stated before. In this case shown you don't have to cast due to rounding of the float prior to the integer truncating the decimal digits.
#include <math.h>
#include <stdio.h>
int main(void) {
float a = 2.4;
float b = 0.2;
int ab = round(a + b);
int ba = round(a - b);
printf("a + b rounded = %i\n", ab);
printf("a - b rounded = %i", ba);
}
output
a + b rounded = 3
a - b rounded = 2
0
Not sure about this