问题
In my app I am dealing with currency which is need to upto two decimal digit , I have used number formatter for this to display up to two decimal place but When I want this double value up like 0.7 to 0.70 , I am unable to to do that , type double is always giving me 0.7 but i want 0.7 to 0.70
回答1:
You can simply do this:
let pi = 3.14159
let text = String(format: "%.2f", arguments: [pi])
print(text) // output = 3.14
回答2:
You should use NSNumberFormatter
:
let numberFormatter = NSNumberFormatter()
numberFormatter.minimumFractionDigits = 2
numberFormatter.maximumFractionDigits = 2
let pi = 3.14159
let str = numberFormatter.stringFromNumber(pi)!
print(str)
来源:https://stackoverflow.com/questions/37630269/how-to-get-a-double-value-up-to-2-decimal-places