0

when I try this code

printf("something %.8f",1.123456789);

It Gives This Result

1.12345679 

There Is No 8 Why Is This

1 Answers1

3

It seems that you expect the specifier "%.8f" to truncate the number at the 8th digit after decimal point. The precision specifier does, however, not truncate digits but rounds the number as stated in this SO answer (please upvote this reference if applicable):

C99 §7.19.6.1 The fprintf function; f,F

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.

Community
  • 1
  • 1
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58