What you want is %.2f, not 2%f.
Also, you might want to replace your %d with a %f ;)
#include <cstdio>
int main()
{
printf("When this number: %f is assigned to 2 dp, it will be: %.2f ", 94.9456, 94.9456);
return 0;
}
This will output:
When this number: 94.945600 is assigned to 2 dp, it will be: 94.95
See here for a full description of the printf formatting options: printf
Printf Questions - Floating Point
c - Is there a way to automatically printf a float to the number of decimal places it has? - Stack Overflow
floating point - 'printf' a float value in C - Stack Overflow
c - Using printf() to output the correct number of decimal places - Stack Overflow
Can I truncate instead of round with printf?
Why does float arithmetic sometimes yield unexpected results?
What is the difference between %f and %lf?
What you want is %.2f, not 2%f.
Also, you might want to replace your %d with a %f ;)
#include <cstdio>
int main()
{
printf("When this number: %f is assigned to 2 dp, it will be: %.2f ", 94.9456, 94.9456);
return 0;
}
This will output:
When this number: 94.945600 is assigned to 2 dp, it will be: 94.95
See here for a full description of the printf formatting options: printf
Use: "%.2f" or variations on that.
See the POSIX spec for an authoritative specification of the printf() format strings. Note that it separates POSIX extras from the core C99 specification. There are some C++ sites which show up in a Google search, but some at least have a dubious reputation, judging from comments seen elsewhere on SO.
Since you're coding in C++, you should probably be avoiding printf() and its relatives.
I am reading The C programming Language book by Brian W. Kernighan and Dennis M. Ritchie, I have a few questions about section 1.2 regarding printf and floating points.
Question 1:
Example:
Printf("%3.0f %6.1f \n", fahr, celsius); prints a straight forward answer:
0 -17.8
20 -6.7
40 4.4
60 15.6
However, Printf("%3f %6.1f \n", fahr, celsius); defaults to printing the first value as 6 decimal points.
0.000000 -17.8
20.000000 -6.7
40.000000 4.4
60.000000 15.6
Q: Why when not specifying the number of decimal points required does it default to printing six decimal points and not none, or the 32-bit maximum number of digits for floating points?
Question 2:
Section 1.2 also mentions that if an operation consists of a floating point and an integer, the integer is converted to floating point for that operation.
Q: Is this only for this operation? Or is it for all further operations within the scope of the function? I would assume only for that one specific operation in that specific function?
If it is in a loop, is it converted for the entire loop or only for that one operation within the loop?
Example:
void function (void) int a; float b; b - a //converts int a to float during operation a - 2 //is int a still a float or is it an integer?
Is there a way to automatically printf a float to the number of decimal places it has?
Use "%g". "%g" lops off trailing zero digits.
... unless the # flag is used, any trailing zeros are removed from the fractional portion of the result and the decimal-point character is removed if there is no fractional portion remaining. C17dr § 7.21.6.1 8.
All finite floating point values are exactly representable in decimal - some need many digits to print exactly. Up to DBL_DECIMAL_DIG from <float.h> (typically 17) significant digits is sufficient - rarely a need for more.
Pass in a precision to encourage enough output, but not too much.
Remember values like 0.00008 are not exactly encoded in the typical binary floating point double, but a nearby value is used like 8.00000000000000065442...e-05
printf("%.*g\n", DBL_DECIMAL_DIG, some_double);
printf("%.17g, %.17g, %.17g, %.17g\n", 1.27, 345.1415926535, 1.22013, 0.00008);
// 1.27, 345.14159265350003, 1.2201299999999999, 8.0000000000000007e-05
DBL_DIG (e.g. 15) may better meet OP's goal.
printf("%.15g, %.15g, %.15g, %.15g\n", 1.27, 345.1415926535, 1.22013, 0.00008);
// 1.27, 345.1415926535, 1.22013, 8e-05
Function to print a double - exactly may take 100s of digits.
sprintf() could help you
There is no direct way to do this in my experience
here is a simple algorithm to help you with this function
munber = float/double input
n = number of decimal places in float/double
char format[999];
sprintf(format ,"%%.%df" ,n);
printf(format, number);
sprintf is like printf but instead of writing to stdout, sprintf writes to a string.
Now you are left with finding number of digits after the precision.
You can also use ".*" with floating points, see also http://www.cplusplus.com/reference/cstdio/printf/ (refers to C++, but the format specifiers are similar)
.number: For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).
...
.*: The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
For example:
float floatnumbervalue = 42.3456;
int numberofdecimals = 2;
printf("%.*f", numberofdecimals, floatnumbervalue);
Output:
42.35
You can use the asterisk for that too, both for the field width and the precision:
printf("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
See e.g. this reference.