Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format printf 2

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Hello Guys

I use the following printf to print the sum of two numbers. I always get the number with a lot of trailing zeros. How can make printf to ignore the zeros if the number is an integer?

23.000000 + 54.000000 = 77.000000

printf("%f + %f = %f",n1,n2,tot");
 
you typecast to change type to integer (in C only)

float a, b, result;

a = 23.00000;
b = 54.00000;
result = a + b;

printf("%d + %d = %d", a, b, result); Hoping to get certified..in C programming.
 
Check out this logic as far as I could get your problem definition. If the no. is integer then,
float num;
int a;
if((num*10)%10==0) //Check if no. is integer.
a = (int) num;
So if the number is an integer then you can type cast it as suggested by bigtamscot.

And it is always a good idea to put precision specifiers as suggested by aphrodita.
 
To SwapSawe,
What if one has 12.05? Regarding your code it will say that it is a integer too.

What I could suggest to do is:
Get rid of the integer part of the number, and check how many digits there are after the point. Then you take 10 to the power of that number of digits and go on with SwapSawe's algorithm.

Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Hi aphrodita,

Thanx for pointing that out....It simply slipped out of my mind.
Thanx again


SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top