1
#include <stdio.h>

int main (){
    float M=2E30, G=6.67E-11, m=6E24, r=1.5E11;

    float F= (G*M*m)/(r*r);

    printf("F is %f",F);
    return 0;
}

I am trying to print the value of F with two decimal precision. Could anyone help me please?

  • 1
    Short answer: replace %f with %.2f. If that's not exactly what you want, look into the various modifiers available in printf. – Simon B Feb 11 '15 at 09:37

2 Answers2

4

For two decimal precision, change your printf statement as below

printf("F is %.2f", F);

read this for more information.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Shivaraj Bhat
  • 841
  • 2
  • 9
  • 20
0

You should try this:

printf("F is %0.2f",F);

The 0 in %0.2f tells that the output need not to be right justified and the 2 after the decimal tells that there should be only 2 digits after the decimal in the output. Basically %0.nf allows us to set a precision of n digits after the decimal in the output.

Hope this helps.

user31782
  • 7,087
  • 14
  • 68
  • 143