Created
May 1, 2015 11:41
-
-
Save samuelbeek/9ac1f8328b2cc119a9a7 to your computer and use it in GitHub Desktop.
Distance Formatter for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DistanceHelper.Swift | |
// Wildcard | |
// | |
// Created by Samuel Beek on 01-05-15. | |
// Copyright (c) 2015 Wildcard. All rights reserved. | |
// | |
import Foundation | |
extension String { | |
mutating func deleteCharactersInRange(range: NSRange) { | |
let startIndex = advance(self.startIndex, range.location) | |
let length = range.length | |
self.removeRange(Range<String.Index>(start: startIndex, end:advance(startIndex, length))) | |
} | |
} | |
func formatDistance(meters: String) -> String { | |
var distance = meters | |
if(count(distance) > 3) { | |
// remove last three characters and make it KM | |
let index = count(distance) | |
let start = index - 3 | |
distance.deleteCharactersInRange(NSMakeRange(start,3)) | |
return "\(distance)km" | |
} else { | |
// Deck is close, so use meters | |
return "\(distance)m" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment