[swift-users] Decimal to Double?

Joe Groff jgroff at apple.com
Mon Nov 28 13:31:14 CST 2016


> On Nov 28, 2016, at 11:29 AM, David Sweeris <davesweeris at mac.com> wrote:
> 
> 
> 
> 
> Sent from my iPhone
>> On Nov 28, 2016, at 13:21, Joe Groff <jgroff at apple.com> wrote:
>> 
>> It really shouldn't be, since ExpressibleByFloatLiteral uses a binary float initial value and so doesn't guarantee decimal accuracy. I'd recommend using init(mantissa:exponent:isNegative:) instead, and minimizing any conversions between Double and Decimal.
> 
> That's what I was worried about... What's the "correct" way to express a literal as a Decimal, then? Use a string literal, `"2.1"`, instead of a float literal, `2.1`?

That would certainly be more robust than using float literals, though probably slower than constructing from elements using the init(mantissa:exponent:isNegative:) initializer.

-Joe


More information about the swift-users mailing list