1

I just started learning C, and I need to take a floating-point input from the user with exactly two numbers after the decimal point and then print it, so I wrote this code:

#include <stdio.h>

int main(void){
    float x;
    scanf("%.2f", &x);
    printf("%f", x);
}

Now say I take 2.14 as input... I don't get 2.14 as output; I get 0.000000. What's wrong with my code??

NOTE: I want the input to be two numbers after the decimal point, and I don't want to do something like:

scanf("%f", x);
printf("%.2f", x);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    What's the return value from `scanf()`? You're ignoring that. Do not ignore return values. – Andrew Henle Jun 16 '16 at 11:42
  • 2
    You do know that the `"%f"` format for [`scanf`](http://en.cppreference.com/w/c/io/fscanf) is for `float`, and that `float` is *single* precision? If you want double precision you should use `double` as the type and `"%lf"` as the [`scanf`](http://en.cppreference.com/w/c/io/fscanf) format. – Some programmer dude Jun 16 '16 at 11:43
  • 1
    `.` modifier is not supported by (at least standard) `scanf()`. Take input as string, verify yourself and convert it to float. – MikeCAT Jun 16 '16 at 11:43
  • 1
    There is no syntax as such. You have to do it by yourself. – 2rd_7 Jun 16 '16 at 11:45
  • By the way, `float x; scanf("%f", x);` will invoke *undefined behavior*, so do not do that. – MikeCAT Jun 16 '16 at 11:51
  • There is no direct way for doing this you cannot bound the user to input only upto 2 digits in the fractional part. Only thing you can do is to reduce the input according to your requirements. – Mayur Kharche Jun 16 '16 at 11:52
  • 2
    You seem to think that "double precision" means "2 fraction digits", but it really does not. This confusion makes the question confusing. – unwind Jun 16 '16 at 11:59
  • here's what i mean: if the user enters 2.123456789 i want the value of x to be 2.12 only –  Jun 16 '16 at 12:16

2 Answers2

2

Use roundf from math.h (solution from Rounding Number to 2 Decimal Places in C):

#include <stdio.h>
#include <math.h>

int main(void){
    float x;
    scanf("%f", &x);
    x = roundf(x * 100) / 100;
    printf("%.2f\n", x);
}

I must add that you suppose the double precision means that there are two digits after the comma; but in C, double precision is double type, vs float that is single precision.

Community
  • 1
  • 1
Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • i tried this solution but it doesn't work..if you enter 2.143 for example it prints 2.140000...i want 2.14 only –  Jun 16 '16 at 12:20
  • @oasma This is just a string representation problem: those zeros are not significant. If you want to print the only two digits after the comma, use `"%.2f"` in `printf`. – Boiethios Jun 16 '16 at 12:23
0

If you don't want to do any operation on your number, then use string. Then you enter the floating point number and display it easily.

If you want to perform any operation, my advice is to use the math library function roundf(). There is even more in that function, so you can round it to a bigger floating point with ceilf() or to a smaller one with floorf().

Typically, you use the type of variable double for double precision:

x =  roundf(x * 100) / 100; // You use this functions like this
x =  ceilf(x * 100) / 100;
x =  floorf(x * 100) / 100;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikola Jokic
  • 103
  • 9