# Exact NSDecimalNumber via literal

**URL:** <https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834>\
**Category:** Pitches\
**Created:** [May 19, 2018, 5:56pm UTC](https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834 "2018-05-19T17:56:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkrajacic](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkrajacic/32/1173_2.png) [@tkrajacic](https://forums.swift.org/u/tkrajacic)\
**Post date:** [May 19, 2018, 5:56pm UTC](https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834/1 "2018-05-19T17:56:12Z")

</div>

I think it should be possible to initialize an `NSDecimalNumber` or `Decimal` from a literal without using a `String`.

I know that this is probably well known but to get the benefits of a decimal number you need to use either a `String` initializer or the one where you specify mantissa and exponent.

```
let literal: NSDecimalNumber = 3.1415926536
// 3.141592653600002
let literalDecimal: Decimal = 3.1415926536
// 3.141592653600000512

let initializer = NSDecimalNumber(value: 3.1415926536)
// 3.141592653600002

// Only those are exact
let exact1 = NSDecimalNumber(mantissa: 31415926536, exponent: -10, isNegative: false)
// 3.1415926536
let exact2 = NSDecimalNumber(string: "3.1415926536")
// 3.1415926536

```

Now that there is a discussion about literal initialization ([Literal initialization via coercion](https://forums.swift.org/t/literal-initialization-via-coercion/11251)) I want to ask if there is any particular reason we can't initialize a decimal number without the `String` literal?

Can't we just have:

```
let literal: NSDecimalNumber = 3.1415926536
// exactly 3.1415926536

```

Am I missing something? The type checker should easily infer the correct type for the literal? Or does it not know what a decimal number is?

---

<div class="post-metadata">

**Author:** ![griotspeak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/griotspeak/32/1966_2.png) [@griotspeak](https://forums.swift.org/u/griotspeak)\
**Post date:** [May 19, 2018, 7:57pm UTC](https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834/2 "2018-05-19T19:57:34Z")

</div>

The issue right now, as I remember it, is that you still end up going "through" a binary representation when transitioning from a literal to a decimal number. I am light on proof right now, but I think that that is the issue right now.

EDIT: Silly me. It says so right there on the tin. `ExpressibleByFloatLiteral` `Float`, here, implies a binary representation

---

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forums.swift.org/u/Martin)\
**Post date:** [May 19, 2018, 8:53pm UTC](https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834/3 "2018-05-19T20:53:49Z")

</div>

Related bug report: [SR-3317  
Literal protocol for decimal literals should support precise decimal accuracy](https://bugs.swift.org/browse/SR-3317), closed as a duplicate of [SR-920  
Re-design builtin compiler protocols for literal convertible types](https://bugs.swift.org/browse/SR-920).

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [August 5, 2019, 8:48pm UTC](https://forums.swift.org/t/exact-nsdecimalnumber-via-literal/12834/4 "2019-08-05T20:48:47Z")

</div>

This has now gone on for so long that there is a proposal to add a Swiftlint rule to discourage use of decimal literals: [Rule Request: discouraged\_decimal\_float\_literal · Issue #2833 · realm/SwiftLint · GitHub](https://github.com/realm/SwiftLint/issues/2833)

Literals that are resolved with less precision than both their textual representation and than is represntable by the target type is not just a small bug, it's a danger.
