You need to save a large integer (greater than the maximum Double value) without loss of accuracy in the program, and then output it to the console.

For example:

x = 135301852344706746049.0 print(String(format:"%f", x)) // 135301852344706744320.000000 
  • Do not use type Double ? - VladD
  • what to use if not double? (in the example of Double) - xhr
  • and what you are not satisfied with the accuracy of Double? - Andrey Iskamov
  • Double accuracy is not enough. In the example above, the number is stored in Double. When outputting, it is clear that a loss of accuracy occurs (see the last significant digits). - xhr

2 answers 2

You can take the library, for example, this:

https://github.com/mkrd/Swift-Big-Integer

Or search others if this one doesn't work.

    There is a better solution - use the Decimal type.

    Example:

     let a: Decimal? = Decimal(string: "135301852344706746049") print(a!) // 135301852344706746049 // нет потери точности let b: Decimal? = Decimal(string: "83621143489848422977") print(a! + b!) // 218922995834555169026 // сложение выполнено верно