本文介绍了如何截断CGFloat值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我注意到 Float CGFloat 在32位和64位设备上的行为不同,这可能会导致错误,因此我尝试替换所有 CGFloat 中的code> Float 值.但是我想不出一种方法来将 CGfloat 值截断为小数点后两位.

since I've noticed that Float and CGFloat behave differently on 32-bit and 64-bit devices, which could lead to bugs, I try to substitute all Float values with CGFloat in my project. However I couldn't figure out a way to truncate CGfloat values to 2 digits after the decimal point.

在我的项目中,我具有一个下载文件的函数,该函数不断返回 Float 值"progress",指示已下载了总文件的百分比,通常类似于"0.3942183693039206".在这种情况下,我需要将数字截断为"0.39",以便可以更新UI,这是我的功能:

In my project I have a function that download files returns a Float value "progress" constantly, indicating how many percent of the total files has been downloaded, which is usually something like "0.3942183693039206". In this case, I need to truncate the number to "0.39" so I can make update to the UI, and here is my function:

    func updatePropertiesForPorgress(progress:CGFloat){
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        let truncated = NSString(format: "%.2f", progress)
        let float = truncated.floatValue
        self.progressTileView.rating = CGFloat(float)
    })
}

它可以很好地与 Float 值配合使用,但是如果我将进度作为 CGFloat 值传递,则截断的变量将等于"0.00":

It works perfectly fine with Float values, but if I pass progress as a CGFloat value, the truncated variable would equal to "0.00":

出于好奇,我试图在操场上复制情况,并成功地做到了.代码很简单:

Out of curiosity, I tried to replicate the situation in playground, and did it successfully. Code as simple as:

var test:CGFloat = 0.3920394829459
let truncated = NSString(format: "%.2f", test)
let float = truncated.floatValue

会有不同的输出,具体取决于"test"变量是 Float 还是 CGFloat 值.如果"test"是 Float 值,则结果将是0.39,这是正确的,但是如果将其更改为 CGFloat ,则结果将是"0.00".

would have different output depending on "test" variable being a Float or a CGFloat value. If "test" is a Float value, the result would be 0.39, which is correct, but if I change it to a CGFloat, the result would be "0.00".

有人可以帮助我了解发生了什么,并告诉我如何正确截断 CGFloat 值吗?我尝试了 NSNumberFormatter ,但是(目前)不起作用.

Can anyone help me understand what's happening, and show me how to truncate CGFloat values correctly? I tried NSNumberFormatter but it didn't work(for now).

谢谢!

推荐答案

NSString(format:_:...)接受 CVarArgType s:

extension NSString {
    convenience init(format: NSString, _ args: CVarArgType...)

但是,从Swift 1.1开始, CGFloat 本身不符合 CVarArgType .传递给 NSString(format:_:...)时,它将转换为 NSNumber .

But, as of Swift 1.1, CGFloat itself does not conforms to CVarArgType. When it passed to NSString(format:_: ...), it is converted to NSNumber.

所以 NSString(格式:%.2f",测试)等同于 NSString(格式:%.2f",测试为NSNumber).结果,%.2f 将为 0.00

So NSString(format: "%.2f", test) is equivalent to NSString(format: "%.2f", test as NSNumber). As a result, %.2f would be 0.00

您应将其明确转换为 Double :

NSString(format: "%.2f", Double(test))


请注意:自Swift 1.2起,由于 CGFloat 符合 CVarArgType ,因此您的代码照常运行.


Note that: as of Swift 1.2, Since CGFloat conforms to CVarArgType, your code works as is.

这篇关于如何截断CGFloat值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 10:17