Reputation: 505
I'm not really sure what I am doing wrong here. I have a double:
let distanceInMiles = distanceInMeters/1609.344
and the results are 2.99685063032388e-09
But I want to round the number to one decimal place, so I do this:
let miles = String(format: "%.1f", distanceInMiles)
But when I print it out miles = 0.0. The decimal place works but it turns my number into 0 instead of 2.9
Upvotes: 0
Views: 323
Reputation: 4270
2.99685063032388e-09
is not equal to 2.9
, it is 0.00000000299685063032388
http://www.wolframalpha.com/input/?i=2.99685063032388e-09
There is actually a cool way to do this, using the exponent format specifier instead of float. (From this list https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html)
let miles = String(format: "%.1e", distanceInMiles)
Upvotes: 2