1
float abc = 20130401.0f;
NSLog(@"abc = %f",abc);

result:

2013-04-08 15:47:38.963 CCLearnIphone[5034:c07] abc = 20130400.000000

It's the same to use double.

How can I express an eight-digit number?

I know precision is lost but .000000 should not be 1.0000000.

%f should be 0.9xxxxx etc

why is it .000000 rounded to 1.000000?

3 Answers3

0

your number is too hight for a float var so you have to change it like double and remove the 'f' character at the end of your number

double abc = 20130401.0;
NSLog(@"abc = %f",abc);
Mirko Catalano
  • 3,850
  • 2
  • 26
  • 39
-1

It's due to the precision of float: about 7 decimal digits.

See https://stackoverflow.com/a/5098949/104790

Community
  • 1
  • 1
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
-1

why is it .000000 rounded to 1.000000?

That's not the case. 20130401 is rounded to 20130400 because 20130400 is the closest value that a float can represent.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • why `float abc = 19999991`, prints to 19999992. Any Hard and fast rule, so that without computer we can tell the result? – Anoop Vaidya Apr 08 '13 at 09:02
  • @AnoopVaidya Without calculating exponent and mantissa it's difficult to tell if a value is exactly representable in a floating point type. The simplest rule of thumb is: Less than 7 decimal digits. – Nikolai Ruhe Apr 08 '13 at 09:06
  • Here's an IEEE 754 converter that shows binary and decimal representations of any entered number: http://www.h-schmidt.net/FloatConverter/ – Nikolai Ruhe Apr 08 '13 at 09:09