0
Rounding float numbers
Hello people, i have a problem whith my program in c language, my profesor asked us about round numbers from 7, for example: if i have 1.54637 my program need to convert that number to 1.54640. i used ceil (number) and floor (number), but this funcions convert my number to integer, but i need the number whith decimals, could you help me whith this problem?
1 Odpowiedź
+ 1
Scaling the value up and down a number of digits can work around that conversion to int issue:
ceil(f * 10000) / 10000.0
Consider using round instead of ceil because round will round up and down when appropriate.
In other words, consider:
round(f * 10000) / 10000.0
More details on round are at:
https://fresh2refresh.com/c-programming/c-arithmetic-functions/c-round-function/