- 1

How to convert an float into a whole number for example 5.5 to 6 and 5.4 to 5

2nd Dec 2021, 1:24 PM
Karthik C
Karthik C - avatar
9 Respostas
+ 1
try using Math.Round(5.5);
2nd Dec 2021, 1:30 PM
cadbrooke
cadbrooke - avatar
+ 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
2nd Dec 2021, 1:30 PM
Ananya | Inactive |
Ananya | Inactive | - avatar
2nd Dec 2021, 1:37 PM
Ananya | Inactive |
Ananya | Inactive | - avatar
+ 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.
2nd Dec 2021, 1:48 PM
Ipang
0
Ok i will try
2nd Dec 2021, 1:31 PM
Karthik C
Karthik C - avatar
0
Ans is not crt
2nd Dec 2021, 1:35 PM
Karthik C
Karthik C - avatar
0
Do u know any other relevant ans
2nd Dec 2021, 1:36 PM
Karthik C
Karthik C - avatar
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
3rd Dec 2021, 12:51 AM
William Owens
William Owens - avatar
0
Not sure about this
4th Dec 2021, 7:58 AM
Priyanshu chouhan
Priyanshu chouhan - avatar