Search code examples
iosobjective-cfloating-pointdouble-precision

Double printing same as float


Why is my double variable storing and printing as a float. Using Xcode.

 #import <limits.h>
 #import <Foundation/Foundation.h>

 long double doubleTest = .123456789101112;
 float floatTest = .123456789101112;

 NSLog(@"Float %f vs Double %Lf", floatTest, doubleTest);

// Output - Float 0.123457 vs Double 0.123457

Solution

  • Try this

    long double doubleTest = .123456789101112;
    float floatTest = .123456789101112;

    NSLog(@"Float %.nf vs Double %.nLf", floatTest, doubleTest);

    replace "n" in the above line with however many number's you'd like to print after decimal point, for instance if you use 6 it will print 6 digits after decimal.

    Hope this helps.