title: Calculating Distance, converting to km and then cutting out decimal places
body: <p>Edit 3:  Thank you beyowulf, I implemented your line of code and this is the <a href="http://i.stack.imgur.com/lAnRs.png" rel="nofollow">result!</a> Exactly what I was hoping for.  Thank you for all the suggestions.</p>
  
  <p>Edit 2:  Interesting, as per user27388882 suggestion I changed my code to:</p>
  
  <pre><code>//convert to kilometers
  let kilometers = Double(round(traveledDistance) / 1000)
  </code></pre>
  
  <p>The result is three decimal places. Ideally I would like only two at most, but this is a step in the right direction! Thank you!</p>
  
  <p>Edit 1: for clarification of "does not work": I guess I can't post a picture, but here is a link when viewed in the simulator. <a href="http://i.stack.imgur.com/HOixe.png" rel="nofollow">The distance still appears as a super long string of decimals, despite using code to try and shorten the number of decimal places. What I perceive to not be working in my code is where the decimal places should be cut off.</a></p>
  
  <p>I am essentially creating an app that tracks a users location while riding their bicycle.  One feature is to take the distance travelled by the user and display it in KM.  I have gotten the distance function to work by searching through other posts.  I have also looked at NSNumberFormatter help documents, but implementing code I have seen does not work.  Is this an issue of distance being a double which is calculated from CLLocation? Another piece of potentially relevant information is that I am working in Xcode 7.2 and Swift2.  </p>
  
  <p>I don't want to post my whole code since I want to highlight where I am stuck, but not sure if more of my code is needed to solve this.</p>
  
  <pre><code>import UIKit
  import CoreLocation
  
  // Global variables
  
  var startLocation:CLLocation!
  var lastLocation: CLLocation!
  var traveledDistance:Double = 0
  
  
  // Identify Labels
  @IBOutlet weak var distanceLabel: UILabel!
  
  // Create a location manager
  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  
  //Calculate the distance between points as a total distance traveled...
  if startLocation == nil {
      startLocation = locations.first! as CLLocation
  } else {
      let lastLocation = locations.last! as CLLocation
      let distance = startLocation.distanceFromLocation(lastLocation)
      startLocation = lastLocation
      traveledDistance += distance
  
      //convert to kilometers
      let kilometers = traveledDistance / 1000
  
  
      //Convert to only two decimal places
      let nf = NSNumberFormatter()
      nf.minimumSignificantDigits = 1
      nf.maximumFractionDigits = 2
      nf.numberStyle = .DecimalStyle
  
      nf.stringFromNumber(kilometers)
  
      //Update the distance label
      self.distanceLabel.text = "\(kilometers) kilometers"
  </code></pre>
  
  <p>Help me, stackOverFlow. You're my only hope.</p>
  
  <p>tl;dr round out decimal places from distance value calculated from user location using swift2.</p>