Из за чего округляется значения с плавающей запятой?
#include <stdio.h>
int main()
{
const double RENT = 3852.99; // константа, объявленная посредоством const
printf("*%.2f*\n", RENT);
printf("*%.1f*\n", RENT); // Почему окргуляется ?
}
В первом выводе функции printf значение не окуглуяется Во втором выводе значение округляется . Почему так ?
Ответы (1 шт):
Так должно работать согласно стандарту для этой функции:
A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
Т.е. в первом случае округления нет, т.к. количество цифр после запятой равно точности, а во втором есть, т.к. больше.