0

I create a Double from a string using:

let doubleValue = Double(value)

The value is 23450000000000000000

And in the debugger I see doubleValue shown as 2.345e+20

And my code has a encoder(to encoder:Encoder) to encode the doubleValue.

try container.encode(doubleValue, forKey: .akey)

When i run it, I see doubleValue is being encode as 2.345e+20. Is there a way to encode the doubleValue in the long format? i.e. 23450000000000000000?

hap497
  • 154,439
  • 43
  • 83
  • 99
  • What difference does it make? (possible XY problem) – Sweeper Sep 29 '20 at 01:17
  • Note that `Double` it is not meant to be used with such large numbers. You will loose precision. For such large numbers not even unsigned numbers like `UInt64` would suffice. You would need to use `Decimal` type and make sure to use the string initializer. – Leo Dabus Sep 29 '20 at 02:27
  • Also when encoding/decoding Decimal types you would need to create your own encoding and decoding methods to preserve its value https://stackoverflow.com/a/62997953/2303865 – Leo Dabus Sep 29 '20 at 02:29

1 Answers1

0

First off, the number you'd like to print out like a long is too big for even a UInt64 (where the max is print(UInt64.max) // 2^64 = 18446744073709551615).

If you really want to print out large values, try using a Decimal type. Here's some code that worked for me:

import UIKit

var str = "Hello, playground"

let doubleValue = Double(23450000000000000000)
Swift.print("hello")
Swift.print(doubleValue)

Swift.print(String(format: "%.0f", doubleValue))
var decimalValue = NSNumber(value: doubleValue).decimalValue
var result = Decimal()
NSDecimalRound(&result, &decimalValue, 0, .plain)
Swift.print(result)

which I dug up by looking at Yervand's answer to this related post

If you try even larger numbers, like "234500000000000000000000" (a few extra zeros in there), then you'll see the value of doing that NSDecimalRound.

If you simply want to print out your doubleValue in lldb, try doing po String(format: "%.0f", doubleValue) at a breakpoint where doubleValue is defined and you should have decent luck.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    I wonder how this answers the question "How to encode Double in the long format". If you change the value to `Double(23450000000000000789)` it will print "23450000000000000000" as well – Leo Dabus Sep 29 '20 at 03:54
  • The way I read the question was that he wanted to print out the value (whether via a `print` or via `po` in lldb), not that he wanted to encode it ala Codable. I guess you're trying to demonstrate the broken precision of big numbers? – Michael Dautermann Sep 29 '20 at 04:56
  • OP said "Is there a way to encode the doubleValue in the long format? i.e. 23450000000000000000?" The answer would be to yes but he would need to use decimal and encode its bytes or encode it as a string. Using `Decimal` wouldn't be enough to solve the encoding issue. related https://stackoverflow.com/a/62997953/2303865 – Leo Dabus Sep 29 '20 at 05:03