Swift双字串 [英] Swift double to string

查看:122
本文介绍了Swift双字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我更新xCode 6之前,我没有问题把一个双重的字符串加到现在它给我错误

Before i updated xCode 6, I had no problems casting a double to a string bur now it gives me error

var a: Double = 1.5
var b: String = String(a)

它给了我错误消息double is not convertible to string。还有其他方式吗?

It gives me the error message "double is not convertible to string". Is there any other way to do it?

推荐答案

它不是转换,它是从一个格式的值创建一个字符串。

It is not casting, it is creating a string from a value with a format.

var a:Double = 1.5
var b:String = String(format:"%f", a)
print("b: \(b)") // b: 1.500000

使用不同的格式:

var c:String = String(format:"%.1f", a)
print("c: \(c)") // c: 1.5

这篇关于Swift双字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆