Swift: 将 String 转换为 Float 并在进行一些数学运算后再次返回 String
Swift: converting String to Float and back to String again after doing some mathematical operations
我一直在谷歌搜索并试图了解 swift 中的 Float 值是如何工作的,但似乎无法理解它,我真的很感激任何帮助,我觉得我只是在浪费我的时间。
例如,假设我有一个 API returns 一些 json 数据,我解析该数据,进行一些计算,然后将一些数据呈现给用户,像这样:
let balance : String = "773480.67" // value that was received through json api
let commission : String = "100000.00" // value that was received through json api
//framework maps the json properties
let floatBalance : Float = Float(balance)! // at this point value is 773480.688
let floatCommission : Float = Float(commission)! //100000.0
//we do some math with the values
let result : Float = floatBalance + floatCommission // this is somehow 873480.687
//and then show some of the values on a label
print("stringBalance: \(balance)") //stringBalance: 773480.67
print("floatBalance: \(floatBalance)") //floatBalance: 773481.0
print("floatCommission: \(floatCommission)") //floatCommission: 100000.0
print("result: \(result)") //result: 873481.0
print("label: \(String(format:"%.2f", result))") //label: 873480.69
print("just kill me now")
我正在使用 EVReflection 框架将 json 属性映射到一个对象,因此从 String 到 Float 的转换是在后台完成的,我没有做太多事情,但是上面显示的值基本上是我正在使用的值。
我的问题是,最后我需要做什么才能从生成的浮点数 (873480.687)
中得到正确的字符串 (873480.67)
还是我的方法从一开始就错了?
谢谢
其实浮点数不能准确表示数字,你得用Double。
这是关于该问题的一个非常好的答案:
编辑:
抱歉,但实际上不应该使用 Double 来执行计算(我从变量的命名假设您正在处理一些银行事务)。以上 linked 答案的那部分确实给出了一个很好的建议:
A solution that works in just about any language is to use integers
instead, and count cents. For instance, 1025 would be .25. Several
languages also have built-in types to deal with money. Among others,
Java has the BigDecimal class, and C# has the decimal type.
我的一位曾经在银行公司工作的同事也证实,所有的计算都是在没有使用 Floats 或 Double 的情况下完成的,而是按照 link.
中的建议使用 Int
我一直在谷歌搜索并试图了解 swift 中的 Float 值是如何工作的,但似乎无法理解它,我真的很感激任何帮助,我觉得我只是在浪费我的时间。
例如,假设我有一个 API returns 一些 json 数据,我解析该数据,进行一些计算,然后将一些数据呈现给用户,像这样:
let balance : String = "773480.67" // value that was received through json api
let commission : String = "100000.00" // value that was received through json api
//framework maps the json properties
let floatBalance : Float = Float(balance)! // at this point value is 773480.688
let floatCommission : Float = Float(commission)! //100000.0
//we do some math with the values
let result : Float = floatBalance + floatCommission // this is somehow 873480.687
//and then show some of the values on a label
print("stringBalance: \(balance)") //stringBalance: 773480.67
print("floatBalance: \(floatBalance)") //floatBalance: 773481.0
print("floatCommission: \(floatCommission)") //floatCommission: 100000.0
print("result: \(result)") //result: 873481.0
print("label: \(String(format:"%.2f", result))") //label: 873480.69
print("just kill me now")
我正在使用 EVReflection 框架将 json 属性映射到一个对象,因此从 String 到 Float 的转换是在后台完成的,我没有做太多事情,但是上面显示的值基本上是我正在使用的值。
我的问题是,最后我需要做什么才能从生成的浮点数 (873480.687)
中得到正确的字符串 (873480.67)
还是我的方法从一开始就错了?
谢谢
其实浮点数不能准确表示数字,你得用Double。 这是关于该问题的一个非常好的答案:
编辑:
抱歉,但实际上不应该使用 Double 来执行计算(我从变量的命名假设您正在处理一些银行事务)。以上 linked 答案的那部分确实给出了一个很好的建议:
A solution that works in just about any language is to use integers instead, and count cents. For instance, 1025 would be .25. Several languages also have built-in types to deal with money. Among others, Java has the BigDecimal class, and C# has the decimal type.
我的一位曾经在银行公司工作的同事也证实,所有的计算都是在没有使用 Floats 或 Double 的情况下完成的,而是按照 link.
中的建议使用 Int