Convert Double to Scientific Notation in swift

I am trying to convert a given double into scientific notation, and running into some problems. I cant seem to find much documentation on how to do it either. Currently I am using:

 var val = 500
 var numberFormatter = NSNumberFormatter()
 numberFormatter.numberStyle = NSNumberFormatterStyle.ScientificStyle
 let number = numberFormatter.numberFromString("\(val)")
 println(number as Double?) 
 // Prints optional(500) instead of optional(5e+2)

What am I doing wrong?

You can set NumberFormatter properties positiveFormat and exponent Symbol to format your string as you want as follow:

let val = 500
let formatter = NumberFormatter()
formatter.numberStyle = .scientific
formatter.positiveFormat = "0.###E+0"
formatter.exponentSymbol = "e"
if let scientificFormatted = formatter.string(for: val) {
    print(scientificFormatted)  // "5e+2"
}

update: Xcode 9 • Swift 4

You can also create an extension to get a scientific formatted description from Numeric types as follow:

extension Formatter {
    static let scientific: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.numberStyle = .scientific
        formatter.positiveFormat = "0.###E+0"
        formatter.exponentSymbol = "e"
        return formatter
    }()
}

extension Numeric {
    var scientificFormatted: String {
        return Formatter.scientific.string(for: self) ?? ""
    }
}

print(500.scientificFormatted)   // "5e+2"

scientific notation to double (swi…, scientific notation to double (swift). Hi, I have a set of numbers formatted as such - 6.11104586446241e-01 - thatt I want to convert to doubles. In Objective C I'd� Scientific notation, also known as standard form is nothing but writing a big number in short form. For example, 1000 could be written as 1 x 10^3. Similarly, we can apply scientific notation even for negative numbers such as, -500 could be written as -0.5 x 10^3. Use our below online scientific notation calculator to convert to and from

The issue is that you are printing the number... not the formatted number. You are calling numberForString instead of stringForNumber

var val = 500
var numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.ScientificStyle
let numberString = numberFormatter.stringFromNumber(val)
println(numberString)

Convert Double to Scientific Notation in swift, I am trying to convert a given double into scientific notation, and running into some problems. I cant seem to find much documentation on how to do it either. scientific notation to double (swift) 2489 Views 2 Replies. Latest reply on Jan 24, 2016 2 I'd rather not have to convert all 10,000 of these guys by hand!

Slight modification to the answer by leo-dabus to Xcode 9 Swift 4:

extension Double {
    struct Number {
        static var formatter = NumberFormatter()
    }
    var scientificStyle: String {
        Number.formatter.numberStyle = .scientific
        Number.formatter.positiveFormat = "0.###E+0"
        Number.formatter.exponentSymbol = "e"
        let number = NSNumber(value: self)
        return Number.formatter.string(from :number) ?? description
    }
}

"Real" scientific notation with string interpolation - Using Swift, I had some trouble with log10 and BinaryFloatingPoint and the escape hatch I used was converting to a Double(). Any better ideas? It seems� Scientific Notation. Not surprisingly, numeric literals can be denoted using the scientific e notation for decimal floats. You could write 7100000000 as 7.1e9, meaning 7.1 times 10 to the 9th power. There's also a notation for hexadecimal floats which uses the p notation.

The Swift Numeric Data Types, In this article, we look at the numeric data types that are available in Swift. Integer Value Limits; Value Ranges of Types; Numeric Type Conversion of precision and the Double type which represents 64-bit floating point numbers at least 15 To write decimal floating point literals in scientific notation we use an upper or� In Swift, we can also write floating point numbers in hexadecimal format and still make use of scientific notation. When writing in hexadecimal format, instead of using an e or E to separate the base value from the exponent we , we use an upper or lowercase letter p. When written in hexadecimal format, the literal value is equivalent to

Numbers and Numeric Types in Swift — Marius Schulz, Read about how working with numbers in Swift differs from other languages. Float (size: 32-bit, precision: ≥6 decimal digits); Double (size: 64-bit, Not surprisingly, numeric literals can be denoted using the scientific e notation for you must opt in to numeric type conversion on a case-by-case basis. 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:

How to convert a double to a string in Swift, Practicality every type (maybe actually every type) in Swift has a “[code ] description[/code]” member variable (of type String), so to get the string of a Double, you� In VB6.0 I receive data as floating point numbers such as 0.00892855 I must convert it to scientific notation such as 8.92855E-03 then write to some file. Format( ) will not handle this.

Comments
  • Can I convert the string back to a double but preserve the scientific notation?
  • Probably not as a Double is by definition not in scientific notation
  • You can create an extension to get a scientific description from your double
  • That is a great idea. Sorry for the nooby questions. I am new to all of this! Thanks for the help ;)
  • Can I convert the string back to a double but preserve the scientific notation? Or no because a double is by definition not in scientific notation?
  • Scientific notation refers to the string you get from the Double. But if you have a string in scientific notation, you can use the formatter to get the double back out of that string