4

Possible Duplicate:
Limit floating point precision?

In C language, I need to format a number to 2 digits after the decimal point from user input

For example:

 float x ; 
 printf("Enter number");

Let's suppose the user enters 4.54234635

I need to print and process in the whole program: 4.54

Thanks advance

Community
  • 1
  • 1
Saeed ALSferi
  • 57
  • 1
  • 1
  • 6

3 Answers3

17
scanf("%.2f",&x);

And i think that will solve a problem

  • You cannot specify precision in a scanf function. But you can do that in a printf function. – jtony Sep 17 '19 at 00:20
13

The complete list

Integer   display
%d      print as decimal integer
%6d     print as decimal integer, at least 6 characters wide
%f      print as floating point
%6f     print as floating point, at least 6 characters wide
%.2f    print as floating point, 2 characters after decimal point

With the required modification (scanf("%.2f",&x); in the last entry) will solve your problem

Shash
  • 4,160
  • 8
  • 43
  • 67
3

use

scanf("%.2f",&number);

or
printf("%.2f", number);

Jah
  • 1,019
  • 1
  • 11
  • 25