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!
For example, if a number is 45.6789, you might only want to show two digits after the decimal place. The "%f" format string means "a floating point number," but "%. 2f" means "a floating-point number with two digits after the decimal point.
format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.
The following code:
import Foundation // required for String(format: _, _) print(String(format: "a float number: %.2f", 1.0321))
will output:
a float number: 1.03
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With