Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi,使用的时候发现:解析大的 Int 数据会失败 #54

Open
Junnor opened this issue Jan 9, 2021 · 3 comments
Open

Hi,使用的时候发现:解析大的 Int 数据会失败 #54

Junnor opened this issue Jan 9, 2021 · 3 comments

Comments

@Junnor
Copy link

Junnor commented Jan 9, 2021

1

` static func number( value: Any, _ type: Any.Type) -> NumberValue? {
guard let str = _numberString(value) else { return nil }
guard let decimal = Decimal(string: str) else { return nil }

    // digit
    if let digitType = type as? DigitValue.Type {
        // 大 Int 数据解析永远是 18035768958676992
        // let x = 18035768958676993,             // 解析失败, 解析结果成 18035768958676992
        // let y = 18035768958676992              // 解析成功
        return Double("\(decimal)")
            .flatMap { NSNumber(value: $0) }
            .flatMap { digitType.init(truncating: $0) }
    }
    
    // decimal number
    if type is NSDecimalNumber.Type {
        return NSDecimalNumber(decimal: decimal)
    }
    
    // decimal
    if type is Decimal.Type { return decimal }
    
    // other
    return Double("\(decimal)").flatMap { NSNumber(value: $0) }
}

`

@Junnor
Copy link
Author

Junnor commented Jan 9, 2021

Double("(decimal)") 的这个解析由于精度问题会返回不一样的值

@zhuangxq
Copy link

image

#46 一样的, 我是这样改的,可以参考一下。

@Junnor
Copy link
Author

Junnor commented Jan 15, 2021

@zhuangxq 你好,感谢提供解决方法。 我目前的解决办法是直接把model里面的 Int 设置成String了,😂,那样精度问题也就不存在了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants