0
var d1 = 0.124 * 0.12456 // 0.01544544
var s1 = String(d1)      // 0.015445440000000001

var d2 = 0.01544544      // 0.01544544
var s2 = String(d2)      // 0.01544544

Why does converting the product of two fractional Doubles to a String result in extraneous zeroes (ending with 1) in the fractional portion of the result, but converting a fractional Double explicitly to a String does not? And how can I remove the extraneous portion from the result?

Kaplan
  • 91
  • 1
  • 11
  • If you are looking for precision you should use Swift `Decimal` type (make sure to use the string initializer). When displaying a value to the user you should use `NumberFormatter` and specify the maximum and minimum fraction digits – Leo Dabus Dec 31 '20 at 05:01
  • Or use `String(format:)` – El Tomato Dec 31 '20 at 05:49

0 Answers0