Swift的 – 如果小数点等于0,如何从浮点数去除小数?

我用一位小数显示距离,如果等于0(如:1200.0Km),我想删除这个小数点,我怎么能这么快速地做到这一点? 我显示这个数字是这样的:

let distanceFloat: Float = (currentUser.distance! as NSString).floatValue distanceLabel.text = String(format: "%.1f", distanceFloat) + "Km" 

这个怎么样?

 let distanceFloat: Float = (currentUser.distance! as NSString).floatValue distanceLabel.text = String(format: distanceFloat == floor(distanceFloat) ? “%.0f" : "%.1f", distanceFloat) + "Km" 

或作为延伸:

 extension Float { var clean: String { return self % 1 == 0 ? String(format: "%.0f", self) : String(self) } } 

extension是强有力的做法。

扩展

 extension Float { var cleanValue: String { return self % 1 == 0 ? String(format: "%.0f", self) : String(self) } } 

用法

 var sampleValue: Float = 3.234 print(sampleValue.cleanValue) 

3.234

 sampleValue = 3.0 print(sampleValue.cleanValue) 

3

 sampleValue = 3 print(sampleValue.cleanValue) 

3

示例游乐场文件在这里 。

使用NSNumberFormatter:

 let formatter = NSNumberFormatter() formatter.minimumFractionDigits = 0 formatter.maximumFractionDigits = 1 let nums = [3.0, 5.1, 7.21, 9.311, 600.0] for num in nums { print(formatter.stringFromNumber(num) ?? "n/a") } 

返回:

3

5.1

7.2

9.3

600

更新为swift接受的答案3

 extension Float { var cleanValue: String { return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self) } } 

用法只是:

 let someValue: Float = 3.0 print(someValue.cleanValue) //prints 3 

NSNumberFormatter是你的朋友

 let distanceFloat: Float = (currentUser.distance! as NSString).floatValue let numberFormatter = NSNumberFormatter() numberFormatter.positiveFormat = "###0.##" let distance = numberFormatter.stringFromNumber(NSNumber(float: distanceFloat))! distanceLabel.text = distance + " Km" 

这是完整的代码。

 let numberA: Float = 123.456 let numberB: Float = 789.000 func displayNumber(number: Float) { if number - Float(Int(number)) == 0 { println("\(Int(number))") } else { println("\(number)") } } displayNumber(numberA) // console output: 123.456 displayNumber(numberB) // console output: 789 

这是最重要的一条线。

 func displayNumber(number: Float) { 
  1. Int(number)去除浮点数的十进制数字。
  2. 将剥离的数字返回到浮点数以执行Float(Int(number))
  3. 获取带数字的十进制数字值number - Float(Int(number))
  4. 检查十进制数字值是否为空, if number - Float(Int(number)) == 0

if和else语句中的内容不需要解释。

你可以使用已经提到的扩展,但是这个解决scheme稍微短一些:

 extension Float { var shortValue: String { return String(format: "%g", self) } } 

用法示例:

 var sample: Float = 3.234 print(sample.shortValue) 

要将其格式化为string,请遵循此模式

 let aFloat: Float = 1.123 let aString: String = String(format: "%.0f", aFloat) // "1" let aString: String = String(format: "%.1f", aFloat) // "1.1" let aString: String = String(format: "%.2f", aFloat) // "1.12" let aString: String = String(format: "%.3f", aFloat) // "1.123" 

要将其格式化为Int,请遵循此模式

 let aInt: Int = Int(aFloat) // "1" 

也许stringByReplacingOccurrencesOfString可以帮助你:)

 let aFloat: Float = 1.000 let aString: String = String(format: "%.1f", aFloat) // "1.0" let wantedString: String = aString.stringByReplacingOccurrencesOfString(".0", withString: "") // "1" 

这是一个扩展应该做的伎俩:

 extension String { var clean: String { var stringCharacters = Array(self.characters) var isDoubleDot = false for character in stringCharacters { if character == "." { isDoubleDot = true break } else { isDoubleDot = false } } if isDoubleDot == true { while 1 == 1 { if stringCharacters[stringCharacters.count-1] == "0" || stringCharacters[stringCharacters.count-1] == "." { stringCharacters.remove(at: stringCharacters.count-1) } else { break } } return String(stringCharacters) } else { return self } } } 

用法:

 let number1 = "54.530000" let number2 = "65.0" let number3 = "21.243" print(number1.clean) //54.53 print(number2.clean) //65 print(number3.clean) //21.243 

这可能也有帮助。

 extension Float { func cleanValue() -> String { let intValue = Int(self) if self == 0 {return "0"} if self / Float (intValue) == 1 { return "\(intValue)" } return "\(self)" } } 

用法:

 let number:Float = 45.23230000 number.cleanValue()