Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:14:07+00:00 2026-06-14T22:14:07+00:00

In my iPhone application, the user can change the unit (km or mile) in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:14:09+00:00Added an answer on June 14, 2026 at 10:14 pm

    If the loss of precision that you have by converting the number one time is acceptable, then I would just quit converting back and forth every time that you display/save it, and only convert, if needed, when the value is entered by the user.

    So in other words, just like in your user defaults, only ever have the value in memory represent km.

    - (void)textFieldDidEndEditing:(UITextField *)textField {
        intTotalKM = [textField.text intValue];
    
        if ([unit isEqualToString:@"mile"]) {
            intTotalKM = intTotalKM / 0.621371192;
        }
    }
    

    Note that this conversion is only done once when the user enters a new value.

    To display the value to the user, you would convert the value to miles if needed. When you save the value, you simply save intTotalKM without doing any conversion since it is always stored in KM.

    Otherwise, you should store the value in such a way that you won’t have this loss of precision. By storing the decimal portion of the number (using a double), by saving as a different unit (like meters – my personal favorite) or by storing the unit and using native values, all as suggested in other answers here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create an iPhone application where the user can add entries.
Can a user install my application from iTunesStore if he is having iPhone OS
I created an iPhone application which can take photos and allow the user to
I am working on an iPhone application of drawing, user can draw a line
I am writing an iPhone application which requires the user to enter several values
In my iPhone application, a user selects an item from a UITableView to choose
I am developing an iPhone application where I collect info from the user when
I have to implement a iphone application which will record user's voice as you
I have 4 tabs in my iphone application. When user will click a button
hi i want to change the text of the iPhone application deletion text Deleting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.