how do i display a floating point number without the decimals.
I.E., how do i make this:
//code:
printf("%f", number);
//output:
1.00000
look like this:
//code
printf("%f", number);
//output
1
Printable View
how do i display a floating point number without the decimals.
I.E., how do i make this:
//code:
printf("%f", number);
//output:
1.00000
look like this:
//code
printf("%f", number);
//output
1
display it as a int not a float.
no no.... you dont understand...... maybe i should make it more clear:
how do i turn this:
//code
printf("%f", number);
//output
99999999999999999999999999999.00000
into this:
//code
printf("%f", number);
//output
99999999999999999999999999999
printf("/i",number) ;
doesnt even work at all....
prints /i
or if you meant \i, it gives an 'unknown escape sequance' error.
casting to int will truncate the fractional part.Code:float number = 1234.56;
printf( "%f", (int)number );
that doesnt work either.
now it just prints the number as ' 0.00000 ' no matter what.
printf("%0.0f", f);
;-)
Sorry, I meant this:
printf( "%d", (int)number );
Well, you could just use:
printf("%.0f",f);
The .0 means 0 digits printed after the decimal point. .1 would mean 1 digit etc.
ahh...
beautiful!
I really should stop visiting this board once I can't remeber the last time I slept(I think it was last tuesday :-)).
For God's sake:
printf("%d",number);
Why is it %d for integers, instead of %i or something?????
PS: I'm ALWAYS fooled that % =96 :)
>Why is it %d for integers, instead of %i or something?????
Both are allowed, but there are differences in how they behave and should be used.
For printf, %d and %i are both flags for signed decimal notation. For scanf, %d is a decimal integer pointer and %i is an integer pointer which may be of a base other than decimal.
%i isn't used often because it's better to be explicit in how you are representing the base with %o, %x, and %d.
Btw,
Fiddle with this a bit, you'll find that you can remove the decimal and anything that follows with a %.0 flag, and a long double will give you a precision up to 17 digits.Code:#include <stdio.h>
int main ( void )
{
long double d = 12345678987634534.0;
printf ( "%.0lf\n", d );
return 0;
}
>printf( "%d", (int)number );
Have you tried this? It works okay as long as the number is small enough to fit in an integer, otherwise you get incorrect output.
>printf("%d",number);
No, and test something before you offer it as a solution.
-Prelude