Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent scientific notation with Float in swift

Tags:

ios

swift

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 ?

like image 648
Morteza Soleimani Avatar asked Aug 15 '15 07:08

Morteza Soleimani


People also ask

How to convert scientific notation to standard form?

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.

How do you convert from float to scientific notation?

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.


3 Answers

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.

like image 144
Ashish Kakkad Avatar answered Nov 28 '22 01:11

Ashish Kakkad


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)
like image 31
vacawama Avatar answered Nov 28 '22 01:11

vacawama


Solution w/ extension (Swift 4+, Xcode 9)

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.

like image 42
Alessandro Francucci Avatar answered Nov 28 '22 02:11

Alessandro Francucci