decimal 转换为String 并制定小数点位数。首先转为double或者float,再用String(format: "%.3f", doubleValue)
即可
let decimalValue: Decimal = 3.14159
let doubleValue = Double(truncating: decimalValue as NSNumber)
print(String(format: "%.3f", doubleValue))
print(String(format: "%.4f", doubleValue))
print(String(format: "%.5f", doubleValue))
print(String(format: "%.6f", doubleValue))
print(String(format: "%.7f", doubleValue))
输出
3.142
3.1416
3.14159
3.141590
3.1415900