Skip to content

[SR-8409] Decimal initialized with Double loses precision #3658

@swift-ci

Description

@swift-ci
Previous ID SR-8409
Radar None
Original Reporter TomasLinhart (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 87fa6caf176bfbf5ed96bbc10110c73b

is duplicated by:

  • SR-11514 Decimal.init(_ value: Double) sometimes yields incorrect value

Issue Description:

Decimal precision is a bit strange for a number 456.789. When you initialize Double with 456.789 it holds the number so it is representable, but if you try to convert it to a decimal with Decimal(456.789) it results in 456.7889999999998976 but if you use Decimal(string: "456.789")! it results in a correct decimal with a value 456.789.

I tried also Objective-C, and it works correctly there:

NSDecimalNumber *number = [[NSDecimalNumber alloc] initWithDouble:456.789];
NSLog(@"Number: %f", number.doubleValue);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions