Does NSInteger allow for decimal places?

Tag: nsinteger , decimal , cgfloat Author: yyjs Date: 2012-11-26

I need to have someone type in a value say "40" and then the app converts this to anther value by dividing by 7.5. at the moment i've done:

    NSString *pCO2 = _pCO2.text;
    float thepCO2 = [pCO2 floatValue];
    NSInteger newpCO2 = thepCO2/7.5;
    NSString *finalpCO2 = [NSString stringWithFormat:@"%d", newpCO2];

and then later, in another view, the app picks up the NSString called finalpCO2 and converts it back to a number like so:

float newpCO2 = ([pCO2 floatValue]);

However, when i do this, values above 38 for the initial pCO2 seem to be being rounded to 5 (38 divided by 7.5 = 5.0666) and below this are being rounded to 4 (37 divided by 7.5 = 4.93)

Does anyone know why this might be happening? I want the figure to be to the nearest decimal place at least.

Thanks

Layth

Other Answer1

NSInteger only stores integers, so no it does not store decimal places. Use a NSFloat or NSDecimal if you need to store floating point values.

comments:

so how would i schange this? can I use 'NSFloat newpCO2 = thepCO2/7.5; NSString *finalpCO2 = [NSString stringWithFormat:@"%d", newpCO2];' Will that now work?
No %d is for integers. Use %f instead. See developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
@shf301 I don't see a NSFloat. There is a CGFLoat though.
you saved my time :-)