Below is how I would have previously truncated a float to two decimal places
NSLog(@" %.02f %.02f %.02f", r, g, b);
I checked the docs and the eBook but haven't been able to figure it out. Thanks!
Below is how I would have previously truncated a float to two decimal places
NSLog(@" %.02f %.02f %.02f", r, g, b);
I checked the docs and the eBook but haven't been able to figure it out. Thanks!
Swift 4
I don't know about two decimal places, but here's how you can print floats with zero decimal places, so I'd imagine that can be 2 place, 3, places ... (Note: you must convert CGFloat to Double to pass to String(format:) or it will see a value of zero)
This is a very fast and simple way who doesn't need complex solution.
Plenty of good answers above, but sometimes a pattern is more appropriate than the "%.3f" sort of gobbledygook. Here's my take using a NumberFormatter in Swift 3.
Here I wanted 2 decimals to be always shown, but the third only if it wasn't zero.
You can still use NSLog in Swift as in Objective-C just without the @ sign.
Edit: After working with Swift since a while I would like to add also this variation
Output:
You can also create an operator in this way