1
let balance = "2.477854178281608e-06" 
// I have to convert this exponential value in Decimal

/* Already tried the below-mentioned solution, but not working */

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let finalNumber = numberFormatter.number(from: balance)
print(finalNumber!)

Value is printing "2.477854178281608e-06\n"

Any help will be appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Soumen
  • 2,070
  • 21
  • 25
  • How many decimal places are you hoping to convert the number to? – kd02 Feb 06 '18 at 16:17
  • `let value = Double(balance)`, probably... – holex Feb 06 '18 at 16:24
  • @kd02 I want up to 5 decimal places. The actual converted value will be this "0.000002477854178281608" which I found by using an online converter. – Soumen Feb 06 '18 at 16:24
  • Know that if you're using 5 decimal places, the value of that number in your example is "0.00000" – Stonz2 Feb 06 '18 at 16:25
  • @holex If you marked as duplicate you should provide a valid link where the solution is given. Just checked your solution but not working. – Soumen Feb 06 '18 at 16:27
  • I see where @holex was going, but I reopened this. Marking it as a duplicate is confusing to searchers IMO. It's not clear how the answer linked matches this question (unless you already know the answer). – Rob Napier Feb 06 '18 at 16:29
  • @Stonz2 Is there any way by which I will get the exact value. which is "0.000002477854178281608" – Soumen Feb 06 '18 at 16:30
  • @Soumen, it was a valid link after... [here](https://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double)... but it has been reopened meanwhile now we have an answer here. – holex Feb 06 '18 at 16:32
  • Those who are referring this link https://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double please read my question carefully once. – Soumen Feb 06 '18 at 16:33

1 Answers1

6
let balance = "2.477854178281608e-06" 
// I have to convert this exponential value in Decimal

This is not an exponential value. This is a string that represents a number using exponential format. You seem to want a string representing the same number in a different format. The important thing here is that neither string is "the value." The value is the same regardless of representation (or approximately the same if the representation is limited).

So first you need the value represented by the string. To do that, convert it to a Double.

let value = Double(balance)!

Now, you say you want to convert that to a string in decimal format (I assume you mean 0.000...). So you need a formatter:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 20
let string = numberFormatter.string(for: value)!
print(string)  // 0.00000247785417828161

You'll note that this value is slightly different than the previous value. That's because there are rounding errors when dealing with values this small.


If all of these base-10 digits are important, you can work with the Decimal type rather than Double. This avoids decimal/binary rounding, but is less convenient and slower for some kinds of math. If this is a type of currency that is expressed in base-10 units (which is basically all of them), you always want to work with Decimal and never with Double.

let balance = "2.477854178281608e-06"
let value = Decimal(string: balance)!

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 21
let string = numberFormatter.string(for: value)!
print(string)  // 0.000002477854178281608
Rob Napier
  • 286,113
  • 34
  • 456
  • 610