Jayaseelan Kumarasamy
Jayaseelan Kumarasamy

Reputation: 33

Double to String conversion issue in some of the iPhone in Objective C

I am facing issue in double to string conversion it occurs one or two users iPhone , from my side it is working as expected

my double value - 10.890000000000001,

expected string output = "10.89",

but some of users iPhone the output is - "1089" ( . is missed)

in debugging I cannot reproduce this, it working as expected, I didn't find any cause of this issue, here I attached my code, Thanks in Advance

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundCeiling];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithDouble:doubleValue]];

Upvotes: 0

Views: 98

Answers (1)

Sumit_VE
Sumit_VE

Reputation: 487

This might not be the best practice but you can try following, it should work for you.

double doubleValue = 10.89;
    
NSString * numberString = [NSString stringWithFormat:@"%.02f", doubleValue];
NSLog(@"numberString = %@", numberString);

Upvotes: 0

Related Questions