hello people am new here.. and i started off coding.. i have few doubts. I want to roundoff a float value with no elements after the decimal point without using a lib function.

For example 16.25 = 16.00 :)

i tried

printf("%.0f,16.25); and it prints 17.. If i know the code for ceil function i could code my own from it.. And i also want to find the location of the decimal point :)

printf("%.0f,16.25); and it prints 17..

not sure what you're talking about.

that will most certainly round it down and print "16"

printf() always prints the float value as a rounded value, to the nearest decimal place that you specify.

if you "really" want to round a value to the nearest decimal place, and store it permanently, then you need to:

muliply the float value by a power of 10, where the log10 of the mulitplier is the number of decimal places you want to round

add +0.5

cast it as an integer to drop all fractional values

divide it by the same power of 10 that you multiplied earlier


double value = 16.75927;
double newVal = (int)(value * 100 + 0.5) / 100.0;

printf("original value             = %f\n\n",value);
printf("value rounded 2 decimal    = %f\n",newVal);

what if i dont want to typecast?

all i want is truncate a given floating point value (e.g.16.25=16). and not assigning the float value to integer & then copy the int value to float and find the position of decimal point in a given floating point value

if you only want to truncate, then do exactly what i said for rounding, but without adding +0.5

if you want a solution to do it without library functions *and* without typecasting, then we're talking about instructors demanding stupid programming tricks for trained monkeys. At which point you're on your own.

floatval= (int) floatval;