How to keep two decimal places in the output of this question in C
You can limit the number of decimal places in the output.
1, when outputting a single precision floating point float variable f, you can use printf(“%.2f”,f) to make the output result retain two significant digits.
2. If it is a double precision floating point number double, you need to use %.2lf to make the output result retain 2 decimal places.
The two types of floating point types are discussed below.
1. Single-precision floating-point type (float)
Single-precision floating-point type (float) refers exclusively to single-precision (single-precision) values that take up 32 bits of storage space. Single-precision is faster than double-precision on some processors and takes up half the space of double-precision, but it becomes imprecise when the value is very large or very small.
2. Double-precision floating-point (double)
Double-precision, as indicated by its keyword “double”, takes up 64 bits of storage space. Double-precision is actually faster than single-precision on some modern processors optimized for high-speed mathematical calculations.
All mathematical functions beyond human experience, such as sin(), cos(), tan(), and sqrt() return double-precision values.
What is the function formula to retain two decimal places?
There are five ways to retain two decimal function formula, respectively:
The first method to set the cell formatting method, select the region of the right mouse button to set the cell format, numerical value, two decimal can be. Characterized by excel cells are displayed in the retained two decimal places, but the actual cell value has not changed.
The second method of clicking on the menu bar button to adjust, characterized by the excel cell is displayed to retain two decimal places, but the actual cell value has not changed.
The third method of retaining two decimal places with the ROUND function, select the C2 cell, enter a function equal to ROUND (A2,2) drop-down fill, adjust the number of decimal places can be. Characterized by the value has become a real rounding results, such as the contents of cell C2, involved in the operation is 1.35, rather than the original 1.345. You can also copy the results, and then pasted into the original region to replace.
The fourth method of retaining two decimal points with the TEXT function, select the C2 cell, enter the function is equal to TEXT (A2, 0.00), pull down to fill. Characterized by the return is to retain two decimal points after the text-based numbers, if you want to become a number can be changed to equal TEXT (A2,0.00), involved in the operation has been rounded up after the number.
The fifth way to use the clipboard or notepad method into a real rounded numbers, in accordance with the first or second method of setting the cell format, copy and paste to notepad, and then cut and paste back into excel form, or copy and paste the clipboard selection can also be.
The sixth method to show the accuracy of the method to become a real rounded numbers, in accordance with the first or second method to set the cell format, click the Office button, Excel Options, Advanced, Calculate this workbook, select the precision is set to the accuracy of the display, to complete the setup, click the OK button.
What should I do to mess up the two decimal places when outputting floating point numbers in C?
The materials you need to prepare are: computer, C compiler.
1, first of all, open the C compiler, create a new initial .cpp file, for example: test.cpp.
2, in the test.cpp file, enter the C code: printf(“%.2f”,3.1415);.
3, the compiler to run test.cpp file, this time the success of the floating-point number retained 2 decimal places after the output.
C printf retains the first 2 decimal places
You can use the “0” flag in the format controller of the printf function to place it in front of the width field, indicating that you use 0 to fill the output. Specifically, you can change the formatting control to “%05.2f”, where “5” indicates that the total width of the output is 5 characters (including the decimal point and decimal portion), “.2” indicates that two decimal places are retained, and “0” indicates that 0 is used to fill the output.
For example, in the following example, we output the value of a floating-point variable d as 00.00:
The output is:
Note that there are two zeros in front of the decimal point in the output here, and two zeros after the decimal point , totaling five characters, matches the description of the format control character above.