Fun programming丨C language realizes outputting 6 decimal places with %f control symbol (source code + comment)

Example: C language implements %f to output real numbers, and can only get 6 decimal places

Problem-solving idea: do not specify the length of the output data, the system determines the number of columns occupied by the data according to the actual situation of the data. The processing method of the system is generally: the integer part of the real number is output, and the decimal part is outputted with 6 digits.

 

Source code demo:

 

 

Compilation result:

 

In the above code, although double_temp is a double-precision type and the result of double_temp/3 is also a double-precision type, the %f format statement can only output 6 decimal places.

Here you can also use %m.nf to specify the data width and the number of decimal places.

In the C language, / is division. If it is an integer division, only the integer part is taken without rounding or the like;% is the modulus, that is, the remainder of the division is taken.

If the modulo is found to be smaller than the divisor, for example:

 

The result of% is that the quotient is 0 and remaining 2, and the output is as follows:

 

The vernacular is that in the modulus, if the dividend is smaller than the divisor, the output value is the dividend itself.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

 

Programming learning video:

 

 

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112741581