Swift中的精确字符串格式说明符

本文翻译自:Precision String Format Specifier In Swift

Below is how I would have previously truncated a float to two decimal places 下面是我之前将浮点数截断为两位小数的方法

NSLog(@" %.02f %.02f %.02f", r, g, b);

I checked the docs and the eBook but haven't been able to figure it out. 我检查了文档和电子书,但还没弄清楚。 Thanks! 谢谢!


#1楼

参考:https://stackoom.com/question/1cuqQ/Swift中的精确字符串格式说明符


#2楼

You can't do it (yet) with string interpolation. 你不能用(或)字符串插值来做。 Your best bet is still going to be NSString formatting: 你最好的选择仍然是NSString格式:

println(NSString(format:"%.2f", sqrt(2.0)))

Extrapolating from python, it seems like a reasonable syntax might be: 从python推断,似乎合理的语法可能是:

@infix func % (value:Double, format:String) -> String {
    return NSString(format:format, value)
}

Which then allows you to use them as: 然后,您可以将它们用作:

M_PI % "%5.3f"                // "3.142"

You can define similar operators for all of the numeric types, unfortunately I haven't found a way to do it with generics. 您可以为所有数字类型定义类似的运算符,遗憾的是我还没有找到使用泛型的方法。


#3楼

My best solution so far, following from David's response : 到目前为止,我的最佳解决方案是David的回应

import Foundation

extension Int {
    func format(f: String) -> String {
        return String(format: "%\(f)d", self)
    }
}

extension Double {
    func format(f: String) -> String {
        return String(format: "%\(f)f", self)
    }
}

let someInt = 4, someIntFormat = "03"
println("The integer number \(someInt) formatted with \"\(someIntFormat)\" looks like \(someInt.format(someIntFormat))")
// The integer number 4 formatted with "03" looks like 004

let someDouble = 3.14159265359, someDoubleFormat = ".3"
println("The floating point number \(someDouble) formatted with \"\(someDoubleFormat)\" looks like \(someDouble.format(someDoubleFormat))")
// The floating point number 3.14159265359 formatted with ".3" looks like 3.142

I think this is the most Swift-like solution, tying the formatting operations directly to the data type. 我认为这是最像Swift的解决方案,将格式化操作直接绑定到数据类型。 It may well be that there is a built-in library of formatting operations somewhere, or maybe it will be released soon. 很可能某个地方有一个内置的格式化操作库,或者它很快就会发布。 Keep in mind that the language is still in beta. 请注意,该语言仍处于测试阶段。


#4楼

a simple way is: 一个简单的方法是:

print(String(format: "hex string: %X", 123456))
print(String(format: "a float number: %.5f", 1.0321))

#5楼

The answers given so far that have received the most votes are relying on NSString methods and are going to require that you have imported Foundation. 到目前为止获得最多投票的答案依赖于NSString方法,并且要求您已经导入了Foundation。

Having done that, though, you still have access to NSLog. 但是,完成后,您仍然可以访问NSLog。

So I think the answer to the question, if you are asking how to continue using NSLog in Swift, is simply: 所以我认为这个问题的答案,如果你问如何继续在Swift中使用NSLog,那就简单地说:

import Foundation


#6楼

A more elegant and generic solution is to rewrite ruby / python % operator: 更优雅和通用的解决方案是重写ruby / python %运算符:

// Updated for beta 5
func %(format:String, args:[CVarArgType]) -> String {
    return NSString(format:format, arguments:getVaList(args))
}

"Hello %@, This is pi : %.2f" % ["World", M_PI]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值