Extra precision values seen for float values

后端 未结 4 1725
旧巷少年郎
旧巷少年郎 2021-01-26 13:38

Below is the solution code for an excercise from KN King\'s Modern Programming approach.

#include

int main(void)
{
    int i;
    float j,x;
             


        
4条回答
  •  终归单人心
    2021-01-26 14:11

    A double is not infinitely accurate, and it does not store numbers in decimal, so it cannot precisely store 0.085. If you want to print a number to two significant figures, use the precision format specifier - see http://www.cprogramming.com/tutorial/printf-format-strings.html for examples.

    double value = 0.085;
    NSLog(@"%.2g", value);
    

    This will print "0.085"

    Be aware that using the precision specifier normally controls the number of digits displayed after the decimal point - but in the case of a double, it controls significant figures.

    float value = 0.085;
    NSLog(@"%.2f", value);
    

    Will print "0.09"

提交回复
热议问题