[swift-users] Decimal to Double?

Alex Blewitt alblue at apple.com
Mon Nov 28 11:22:38 CST 2016


NSDecimal conforms to the ExpressibleBy(Float|Integer)Literal, but that just means it can be created from a literal value, not the other way around:

https://github.com/apple/swift-corelibs-foundation/blob/108a5b0006912c6cf6b59e4954255237bf01b67a/Foundation/NSDecimal.swift#L319-L329 <https://github.com/apple/swift-corelibs-foundation/blob/108a5b0006912c6cf6b59e4954255237bf01b67a/Foundation/NSDecimal.swift#L319-L329>



extension Decimal : ExpressibleByFloatLiteral {
    public init(floatLiteral value: Double) {
        self.init(value)
    }
}

extension Decimal : ExpressibleByIntegerLiteral {
    public init(integerLiteral value: Int) {
        self.init(value)
    }
}

It would be relatively simple for the NSDecimal to add an extension initialiser to Double to allow construction directly from an NSDecimal, but it would have to be done for both the Linux and Darwin implementations.

Alex

> On 28 Nov 2016, at 17:15, David Sweeris <davesweeris at mac.com> wrote:
> 
> 
> On Nov 28, 2016, at 09:48, Philippe Hausler via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> 
>> This might be a bit nicer since that is relying on NSNumber bridges. You can bridge to NSDecimalNumber directly like this: 
>> 
>> (Decimal(1.0) as NSDecimalNumber).doubleValue
>> 
>> (but perhaps we should consider adding initializers that follow the same pattern as Double that don’t have to bridge to solve the problem)
>> 
>>> On Nov 28, 2016, at 2:40 AM, Alex Blewitt via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>> 
>>> You can wrap it with an NSDecimalNumber, and then cast it to a Double from there:
>>> 
>>> Double(NSDecimalNumber(decimal:Decimal(1.0)))
>>> 
>>> Alex
>>> 
>>>> On 28 Nov 2016, at 10:13, Rick Mann via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>> 
>>>> How do I get a Double from a Decimal?
>>>> 
>>>> TIA,
>>>> 
>>>> -- 
>>>> Rick Mann
>>>> rmann at latencyzero.com <mailto:rmann at latencyzero.com>
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-users mailing list
>>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>>> 
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>> 
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> Does NSDecimalNumber conform to the floating point literal whosywhatsit protocol? If not, wouldn't both of those examples just covert a Double to a decimal and back again? Would there be a purpose to that? Like maybe it'd get rounded differently or something?
> 
> Once you have an NSDecimalNumber variable, though, I don't know of any better way than that '.doubleValue' property you and Alex mentioned.
> 
> - Dave Sweeris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161128/a59480bb/attachment.html>


More information about the swift-users mailing list