How are you doing it in Visual Studio? It probably is very close to the code in Swift. Here is a crude example:
let i = floor(value) let f = value - i let s = "\(Int(i)) \(Int(rint(f*8)))/8"
Did you also want the other question answered: "How to parse a string for "." and get its index position?"?
ahltorp, Rather than relying on old school C API for this (
rint), you might want to familiarise yourself with Swift 3’s new
FloatingPointprotocol (SE-0067), which has a bunch of neat-o rounding support, like:
print(2.1.rounded()) // 2.0 print(1.9.rounded()) // 2.0 print(1.5.rounded()) // 2.0 print(-1.5.rounded()) // -2.0 print(1.5.rounded(.towardZero)) // 1.0 print(-1.5.rounded(.towardZero)) // -1.0
@Terry, if you plan to display this string to the user, you should use
NumberFormatterto render number values rather than string interpolation. This will do the right thing regardless of the user’s locale.
Hmmm, then again, if you’re working in eighths of an inch you probably only have one locale you need to work about (-;
Share and Enjoy
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"