Looks like -[NSString doubleValue]
isn't respecting the locale decimal separator; it expects a point, not a comma.
// US uses point as decimal separator
[doubleValueWithMaxDecimalDigits setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
NSString *valueToReturn = [doubleValueWithMaxDecimalDigits stringFromNumber:num];
// Correctly includes fractional part
NSLog(@"Double value converted from string %lf", [valueToReturn doubleValue]);
// Italy uses a comma
[doubleValueWithMaxDecimalDigits setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"]];
valueToReturn = [doubleValueWithMaxDecimalDigits stringFromNumber:num];
// Drops fractional part
NSLog(@"Double value converted from string %lf", [valueToReturn doubleValue]);
I'm not sure why you're formatting the number into a string that you don't use, but if you're going to continue to do that, use the same formatter again to parse the string into a double
before returning.
(Aside, your format specifier is wrong; use %lf
for double
.)