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 Xcode 10 Update
You can't do it (yet) with string interpolation. Your best bet is still going to be NSString formatting:
Extrapolating from python, it seems like a reasonable syntax might be:
Which then allows you to use them as:
You can define similar operators for all of the numeric types, unfortunately I haven't found a way to do it with generics.
Also with rounding:
@Christian Dietrich:
instead of:
it could also be:
[correction:] Sorry for confusion* - Of course this works with Doubles. I think, most practical (if you want digits to be rounded, not cut off) it would be something like that:
For Float only, simply replace Double with Float, pow with powf and round with roundf.
Update: I found that it is most practical to use return type Double instead of String. It works the same for String output, i.e.:
prints: Pi is roughly 3.142
So you can use it the same way for Strings (you can even still write: println(d ~> 2)), but additionally you can also use it to round values directly, i.e.:
or whatever you need …
use below method