简体   繁体   English

swift中double的错误值

[英]Wrong values for double in swift

I have string value and converting to double value for adding it. 我有字符串值并转换为double值以添加它。 But it shows different values when debugging. 但它在调试时显示不同的值。 While using print statement it shows correct values, but when using "po" command in console shows different, so getting different results then expected one. 虽然使用print语句它显示正确的值,但是当在控制台中使用“po”命令显示不同时,所以获得不同的结果然后预期一个。

    let stringValue = "83.84"
    let doubleValue = Double(stringValue)
    let floatValue = Float(stringValue)

    let decimalArray: Array<Double> = [doubleValue!, doubleValue!, doubleValue!]
    let test1 = decimalArray.reduce(0, combine: +)

    let floatArray: Array<Float> = [floatValue!, floatValue!, floatValue!]
    let test2 = floatArray.reduce(0, combine: +)

    print(test1)
    print(test2)

I have attached the image of the console. 我附上了控制台的图像。 Please let me know how this can be resolved. 请让我知道如何解决这个问题。 在此输入图像描述

I think you are using po before it's properly converting. 我认为你在正确转换之前使用的是po put just one breakpoint at last line ie at print(test2) and then check. 在最后一行只放一个breakpoint ,即print(test2) ,然后检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM