Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to deal with decimals in C language? Why are the output results of the following programs different from the actual ones? thank you
How to deal with decimals in C language? Why are the output results of the following programs different from the actual ones? thank you
Decimals cannot be accurately stored in floating-point variables.

When floating-point variable assignment is combined with integer variable, it is necessary to add 0.5 before assignment for rounding.

The display can be set to three decimal places.

printf("\nspeed=%.3f\n ",speed);

If you really need a very accurate decimal and the numerical range is not very large, you can consider using integer variables instead of floating-point numbers.

For example, uint speed=32 1 123 is commonly used in applications to represent floating-point numbers 32 1. 123, such as variables representing prices.