将小数加倍到2位小数

时间:2021-10-07 17:10:49

How do I round up currentRatio to two decimal places?

如何将currentRatio四舍五入到小数点后两位?

let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = "\(currentRatio)"

8 个解决方案

#1


65  

Use a format string to round up to two decimal places and convert the double to a String:

使用格式字符串将最多两位小数舍入并将double转换为String:

let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)

Example:

例:

let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"

If you want to round up your last decimal place, you could do something like this (thanks Phoen1xUK):

如果你想要整理你的最后一个小数位,你可以做这样的事情(感谢Phoen1xUK):

let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"

#2


7  

Updated to SWIFT 4 and the proper answer for the question

更新了SWIFT 4以及该问题的正确答案

If you want to round up to 2 decimal places you should multiply with 100 then round it off and then divide by 100

如果你想要舍入到2位小数,你应该乘以100然后将其四舍五入然后除以100

var x = 1.5657676754 
var y = (x*100).rounded()/100
print(y)  // 1.57 

#3


3  

Consider using NumberFormatter for this purpose, it provides more flexibility if you want to print the percentage sign of the ratio or if you have things like currency and large numbers.

考虑使用NumberFormatter来实现此目的,如果您想打印比率的百分比符号或者您有货币和大数字等内容,它可以提供更大的灵活性。

let amount = 10.000001
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
let formattedAmount = formatter.string(from: amount as NSNumber)! 
print(formattedAmount) // 10

#4


3  

Adding to above answer if we want to format Double multiple times, we can use protocol extension of Double like below:

如果我们要多次格式化Double,可以添加上面的答案,我们可以使用Double的协议扩展,如下所示:

extension Double {
    var dollarString:String {
        return String(format: "$%.2f", self)
    }
}

let a = 45.666

print(a.dollarString) //will print "$45.67"

#5


1  

String(format: "%.2f", Double(round(1000*34.578)/1000))

Output: 34.58

产量:34.58

#6


1  

The code for specific digits after decimals is:

小数后面的特定数字的代码是:

var roundedString = String(format: "%.2f", currentRatio)

Here the %.2f tells the swift to make this number rounded to 2 decimal places.

这里%.2f告诉swift将这个数字四舍五入到小数点后两位。

#7


0  

Just single line of code:

只需一行代码:

 let obj = self.arrayResult[indexPath.row]
 let str = String(format: "%.2f", arguments: [Double((obj.mainWeight)!)!])

#8


0  

if you give it 234.545332233 it will give you 234.54

如果你给它234.545332233它会给你234.54

let textData = Double(myTextField.text!)!
let text = String(format: "%.2f", arguments: [textData])
mylabel.text = text

#1


65  

Use a format string to round up to two decimal places and convert the double to a String:

使用格式字符串将最多两位小数舍入并将double转换为String:

let currentRatio = Double (rxCurrentTextField.text!)! / Double (txCurrentTextField.text!)!
railRatioLabelField.text! = String(format: "%.2f", currentRatio)

Example:

例:

let myDouble = 3.141
let doubleStr = String(format: "%.2f", myDouble) // "3.14"

If you want to round up your last decimal place, you could do something like this (thanks Phoen1xUK):

如果你想要整理你的最后一个小数位,你可以做这样的事情(感谢Phoen1xUK):

let myDouble = 3.141
let doubleStr = String(format: "%.2f", ceil(myDouble*100)/100) // "3.15"

#2


7  

Updated to SWIFT 4 and the proper answer for the question

更新了SWIFT 4以及该问题的正确答案

If you want to round up to 2 decimal places you should multiply with 100 then round it off and then divide by 100

如果你想要舍入到2位小数,你应该乘以100然后将其四舍五入然后除以100

var x = 1.5657676754 
var y = (x*100).rounded()/100
print(y)  // 1.57 

#3


3  

Consider using NumberFormatter for this purpose, it provides more flexibility if you want to print the percentage sign of the ratio or if you have things like currency and large numbers.

考虑使用NumberFormatter来实现此目的,如果您想打印比率的百分比符号或者您有货币和大数字等内容,它可以提供更大的灵活性。

let amount = 10.000001
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
let formattedAmount = formatter.string(from: amount as NSNumber)! 
print(formattedAmount) // 10

#4


3  

Adding to above answer if we want to format Double multiple times, we can use protocol extension of Double like below:

如果我们要多次格式化Double,可以添加上面的答案,我们可以使用Double的协议扩展,如下所示:

extension Double {
    var dollarString:String {
        return String(format: "$%.2f", self)
    }
}

let a = 45.666

print(a.dollarString) //will print "$45.67"

#5


1  

String(format: "%.2f", Double(round(1000*34.578)/1000))

Output: 34.58

产量:34.58

#6


1  

The code for specific digits after decimals is:

小数后面的特定数字的代码是:

var roundedString = String(format: "%.2f", currentRatio)

Here the %.2f tells the swift to make this number rounded to 2 decimal places.

这里%.2f告诉swift将这个数字四舍五入到小数点后两位。

#7


0  

Just single line of code:

只需一行代码:

 let obj = self.arrayResult[indexPath.row]
 let str = String(format: "%.2f", arguments: [Double((obj.mainWeight)!)!])

#8


0  

if you give it 234.545332233 it will give you 234.54

如果你给它234.545332233它会给你234.54

let textData = Double(myTextField.text!)!
let text = String(format: "%.2f", arguments: [textData])
mylabel.text = text