I have the following code :
rangeSlider.minLabel?.text = "\(rangeSlider.lowerValue)"
The label text is 1e+07 but I want to be 100000000.
How should I disable scientific notation ?
Steps for Converting Scientific Notation to Standard FormStep 1: Identify the exponent in the power of 10. Step 2: Move the decimal that many places to the right if the exponent is positive and to the left if the exponent is negative. Step 3: Fill in any empty spaces with zeros.
To convert a floating decimal point number to scientific notation you need to multiply or divide the pre-exponential by 10 to a power, until there is just one digit to the left of the decimal point.
Format your number style :
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
let finalNumber = numberFormatter.number(from: "\(rangeSlider.lowerValue)")
print(finalNumber!)
With the conversion of simple 1e+07
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = NumberFormatter.Style.decimal
let finalNumber = numberFormatter.number(from: "\(1e+07)")
print(finalNumber!)
Output :
10000000
Hope this helps.
Another approach is to use String(format:)
which is available if you have Foundation
imported:
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)
I've just had the same problem to face. For displaying this kind of numbers in a string, I've created the following extension:
extension Double {
func toString(decimal: Int = 9) -> String {
let value = decimal < 0 ? 0 : decimal
var string = String(format: "%.\(value)f", self)
while string.last == "0" || string.last == "." {
if string.last == "." { string = String(string.dropLast()); break}
string = String(string.dropLast())
}
return string
}
}
Usage example:
var scientificNumber: Double = 1e+06
print(scientificNumber.toString()) // 1000000
scientificNumber = 1e-06
print(scientificNumber.toString()) // 0.000001
scientificNumber = 1e-14
print(scientificNumber.toString()) // 0 (too small for the default tollerance.)
print(scientificNumber.toString(decimal: 15)) // 0.00000000000001
For floats works as well. Just extend Float instead of Double.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With