How to prevent scientific notation with Float in swift

I have the following code:

rangeSlider.minLabel?.text = "\(rangeSlider.lowerValue)" 

The label text is 1e + 07 , but I want to be 100000000.

How to disable scientific notation?

+4
source share
2 answers

Format the style of the number:

 let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle let finalNumber = numberFormatter.numberFromString("\(rangeSlider.lowerValue)") print(finalNumber!) 

With conversion simple 1e + 07

 let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle let finalNumber = numberFormatter.numberFromString("\(1e+07)") print(finalNumber!) 

Exit:

10,000,000

Hope this helps.

+6
source

Another approach is to use String(format:) , which is available if you imported Foundation :

Example:

 import Foundation // this comes with import UIKit or import Cocoa let f: Float = 1e+07 let str = String(format: "%.0f", f) print(str) // 10000000 

In your case:

 rangeSlider.minLabel?.text = String(format: "%.0f", rangeSlider.lowerValue) 
+4
source

All Articles