In swift I know how to set the number of digits after the decimal point when converting a double to a string:
String(format: "%0.2f", someDouble)
Similarly I know how to set the number of digits before the decimal point:
String(format: "%02d", someDouble)
But how can I do both?
I want the string to always have a 00.00 format.
Thanks
You simply combine the two:
String(format: "%05.2f", someDouble)
The 0
means fill with leading zeros as needed.
The 5
means you want the final output to be at least 5 characters, include the decimal point.
The .2
means you want two decimal places.
If this is a number you are showing to a user then you should probably use NumberFormatter
so the decimal is properly formatted for the user's locale.
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