Input decimal numbers in C [duplicate] - c

This question already has answers here:
How to only accept a certain precision (so many decimals places) in scanf?
(4 answers)
Closed 7 years ago.
I am extremely beginner in programming language. so i am facing some problems. please help me out.
Is it possible to take input a floating or double number with 2 digits after the decimal point using 'scanf' in C ??

See here: How to only accept a certain precision (so many decimals places) in scanf?
float value;
scanf("%4f", &value);
It's not really do that, but reads 4 digit edit: 4 characters float number. You can set other number instead of 4.
If you really need only 2 decimal places you can read the number with scanf, and after that round it using roundf.
#include <math.h>
...
float value;
scanf("%f", &value);
value = roundf(value*100)/100

You can read floats with
float a;
scanf("%f", &a);
You can read doubles with
double a;
scanf("%lf", &a);

Related

I made a simple C program to receive a float as input from user and display it in screen. But the value changes by a small amount while printing [duplicate]

This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Closed 1 year ago.
I made a simple C program to get a float as input from the user using scanf() function and dispay it using printf() function. But when the input number is quite large, the output deviates slightly. Why might that happen?
For example: if I give input as 2345, out put will be as expected. But if I give the output as 1234567898, the output will be unexpected.
#include <stdio.h>
int main()
{
float number;
scanf("%f", &number);
printf("The number %f", number);
return 0;
}
Single precision floating point (float) is good for approximately 6 significant decimal digits. double allows 15 significant figures. 1234567898.0 has 10 significant figures. Only the most significant first 6 digits can be relied on.

in c, asking to scanf 1 digit after point and getting gybrish [duplicate]

This question already has answers here:
How to only accept a certain precision (so many decimals places) in scanf?
(4 answers)
Closed 5 years ago.
i am wishing to scan a float with 1 digit accuration after FP.
this is the test code:
#include <stdio.h>
void main()
{
float f;
scanf("%.1f",&f);
printf("%f",f);
}
i have entered 1.234, expecting to see 1.2 but instead
i was seeing this weird result :
-107374176.000000
running VS2010, any ideas how to fix please?
It prints random numbers because the call to scanf failed and you are printing an uninitialized variable.
The floating point precision specification only applies in calls to printf, not to scanf. The format specifier you gave is simply invalid.
You would have known the call failed, had you checked the return value of scanf, which is equal to the number of format specifiers that were successfully converted.
If you want a specific accuracy for the number, you'd need to read it in full and then preform the truncation/rounding operation.
You can't do that, you can make scanf() read a specific number of characters if you want like
float value;
scanf("%4f", &value);
and suppose the input is
43.23
it will read
43.2
but you can't specify precision.
what you can do is
float value;
if (scanf("%f", &value) == 1)
printf("%.2f\n", value);
after all, the precision is limited by the binary representation, so making it have only two decimal places is pointless since in arithmetic operations it might be rounded.
scanf("%.1f",&f);
Your format specifier in the scanf() function is incorrect; you cannot specify the number of decimal places you want in as input for scanf(), you can only do that when you want to output the floating point number using printf().
You need to accept the input in full with scanf():
scanf("%f",&f);
And then print out the number you want to the desired number of decimal places:
printf("%.1f", f);
This will print out 1.234 as 1.2.
Also, main() needs to have a return type of int by default; void main() is not legal in C or C++. Even if you do not want to return anything from main(), define it as:
int main(void)

Floating point number is rounding off in C [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
i started learning c. Today, while i am working on a program, i found an interesting thing and i made another small program (similar to my issue) to check it.
#include<stdio.h>
int main(void)
{
float num1=867.0;
float num2=.6921;
printf("sum = %.4f \n",num1+num2);
return 0;
}
if i run the above program, i am getting 867.6921 as the answer. but if i changed the .4%f to %f the answer is changing to sum = 867.692078. why's the change in the output? Also, is there any way that i can get the answer without using the .4%f?
but if i changed the .4%f to %f the answer is changing to sum = 867.692078
printf rounds the result to that many digits after the floating point you specified, the default is .6. While rounding, it uses the current floating point rounding mode, the default is round to the nearest.
The float type has about seven digits of precision (can be anywhere from 6-9 digits). One alternative is to use g instead of f. This fixes the number of precise digits in the number, not just those after the decimal.
printf("sum = %.7g \n",num1+num2);
This produces output
sum = 867.6921
and will always give seven digits of precision for any input. Number formats would be like :
0.000000E+07
0000000
000000.0
00000.00
0000.000
000.0000
...etc

Float value input error [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
With working with floating point values I don't know why my C compiler or the System is behaving unexpectedly. Not making it so confusing. Here is a simple program to take float value input and print the same.
#include <stdio.h>
int main()
{
float num;
printf("Enter float value ");
scanf("%f", &num);
printf("Value = %f", num);
return 0;
}
On multiple run of the same code giving me different and weird outputs. Here are some sample runs.
Sample Run #1 Correct Output
Enter float value 12.0312
Value = 12.031200
Sample Run #2 Incorrect and unexpected output
Enter float value 324.123
Value = 324.122986
Sample Run #3 Incorrect and unexpected output
Enter float value 945.1234
Value = 945.123413
I don't know why the system is adding some garbage values at the end. I am using GNU GCC compiler in code::blocks.
Your float cannot represent every conceivable number exactly. It has limitations. The closest float to "324.123" is 324.122986...
If you need more precision, use double, but even that has its limits too.
A float will be correct (match similarly limited input) to at least FLT_DIG significant digits. To print a number to FLT_DIG significant digits use:
printf("%.*e", FLT_DIG - 1, num);
FLT_DIG is at least 6.
number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits, ... C11dr ยง5.2.4.2.2 11
printf("Value = %f", num);
It will add some value if like :
Enter float value 945.1234
Value = 945.123413
because float value always print a 6 digit after the decimal point
to avoid this you can use the statement like :
printf("Value = %.3f", num);
This Will Give You A desire output as you want
it will print only three number after the decimal point
you can set as per your requirement like
"%.2f" ->for 2 number after the decimal
"%.3f" ->for 3 number after the decimal
"%.4f" ->for 4 number after the decimal

C language how to format a double to 2 digits after the decimal point? [duplicate]

This question already has answers here:
Closed 10 years ago.
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
scanf("%.2f",&x);
And i think that will solve a problem
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
use
scanf("%.2f",&number);
or
printf("%.2f", number);

Resources