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!
here a "pure" swift solution
A version of Vincent Guerci's ruby / python % operator, updated for Swift 2.1:
I found
String.localizedStringWithFormat
to work quite well:Example:
Most answers here are valid. However, in case you will format the number often, consider extending the Float class to add a method that returns a formatted string. See example code below. This one achieves the same goal by using a number formatter and extension.
The console shows:
SWIFT 3.1 update
Swift2 example: Screen width of iOS device formatting the Float removing the decimal
A more elegant and generic solution is to rewrite ruby / python
%
operator: