Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i want to use syntax like %.2f which means it has to print upto two decimal places but its not taking like that instead of that it is not even reading as float value why?

What I have tried:

printf("ans=%.2f\n",a/b)
Posted
Updated 21-Feb-17 8:44am
v2
Comments
Richard MacCutchan 21-Feb-17 14:26pm    
What types are a and b?
Member 13015247 21-Feb-17 14:41pm    
a and b are declared as floats initially and ya this problem is occuring in cs50ide why? in codeblock its running good
Richard MacCutchan 21-Feb-17 14:43pm    
So your actual issue is that cs50ide does not work. Pity you did not state that in the first place.

Probably because your code is using integer values for a and b. Try declaring them as float, not int:
#include <stdio.h>

int main()
{
    float a = 24;
    float b = 7;
    printf("Hello, World!\n");
    printf("ans = %.2f\n", a / b);
    printf("ans = %2.2f\n", a / b);
    return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 21-Feb-17 14:43pm    
See comments above.
OriginalGriff 21-Feb-17 14:57pm    
See his other question...:laugh:
Member 13015247 21-Feb-17 15:03pm    
for which ques r u talking?
Richard MacCutchan 21-Feb-17 15:41pm    
All of them.
Member 13015247 21-Feb-17 15:05pm    
if u can solve the problem then solve it otherwise laugh somewhere else this is not the place to laugh
Make sure a/b is a float.
If a and b are integers, / result in an integer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900