In my iPhone application, the user can change the unit (km or mile) in the preferences.
[[NSUserDefaults standardUserDefaults] setObject:@"km" forKey:@"distanceUnit"];
or
[[NSUserDefaults standardUserDefaults] setObject:@"mile" forKey:@"distanceUnit"];
I save the distance only in km in my Core-Data. I work with int. So every time I get the distance i do the following:
if ([unit isEqualToString:@"km"]) {
} else {
intTotalKM = intTotalKM * 0.621371192;
}
And when saving the distance, I do the following:
if ([unit isEqualToString:@"km"]) {
} else {
intTotalKM = intTotalKM / 0.621371192;
}
For example, without changing my distance, when I save and get it, I loose 1 or 2 km/miles.
I loose precision because of 0.621371192 and because I'm using only int. I don't want to use double values. Do you have any ideas of how to deal with this problem?
Edit: I wonder if there is any other way of conversion more appropriate for my situation.
Thanks.