NumberFormatter string(from:NSNumber) method has issues with decimal places










1















Here is my code



let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18 //This is a variable value
ns.minimumFractionDigits = 18 //This is a variable value
ns.roundingMode = .floor
ns.numberStyle = .decimal
let doubleValueOfDecimal : Double = 12.95699999999998
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(string)


The following is the output and input

doubleValueOfDecimal = 2.95699999999998
Output
2.95699999999998
Optional("2.956999999999980000")

But if I input



doubleValueOfDecimal = 12.95699999999998


The output is



12.95699999999998
Optional("12.957000000000000000")


The string conversion rounds up the value.
Can someone explain me how this works?



The string conversion is rounding up the decimal places when I want it to show the exact number.










share|improve this question
























  • Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

    – CRD
    Nov 12 '18 at 8:10











  • Possible duplicate of Is floating point math broken?

    – Gereon
    Nov 12 '18 at 9:31















1















Here is my code



let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18 //This is a variable value
ns.minimumFractionDigits = 18 //This is a variable value
ns.roundingMode = .floor
ns.numberStyle = .decimal
let doubleValueOfDecimal : Double = 12.95699999999998
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(string)


The following is the output and input

doubleValueOfDecimal = 2.95699999999998
Output
2.95699999999998
Optional("2.956999999999980000")

But if I input



doubleValueOfDecimal = 12.95699999999998


The output is



12.95699999999998
Optional("12.957000000000000000")


The string conversion rounds up the value.
Can someone explain me how this works?



The string conversion is rounding up the decimal places when I want it to show the exact number.










share|improve this question
























  • Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

    – CRD
    Nov 12 '18 at 8:10











  • Possible duplicate of Is floating point math broken?

    – Gereon
    Nov 12 '18 at 9:31













1












1








1








Here is my code



let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18 //This is a variable value
ns.minimumFractionDigits = 18 //This is a variable value
ns.roundingMode = .floor
ns.numberStyle = .decimal
let doubleValueOfDecimal : Double = 12.95699999999998
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(string)


The following is the output and input

doubleValueOfDecimal = 2.95699999999998
Output
2.95699999999998
Optional("2.956999999999980000")

But if I input



doubleValueOfDecimal = 12.95699999999998


The output is



12.95699999999998
Optional("12.957000000000000000")


The string conversion rounds up the value.
Can someone explain me how this works?



The string conversion is rounding up the decimal places when I want it to show the exact number.










share|improve this question
















Here is my code



let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18 //This is a variable value
ns.minimumFractionDigits = 18 //This is a variable value
ns.roundingMode = .floor
ns.numberStyle = .decimal
let doubleValueOfDecimal : Double = 12.95699999999998
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(string)


The following is the output and input

doubleValueOfDecimal = 2.95699999999998
Output
2.95699999999998
Optional("2.956999999999980000")

But if I input



doubleValueOfDecimal = 12.95699999999998


The output is



12.95699999999998
Optional("12.957000000000000000")


The string conversion rounds up the value.
Can someone explain me how this works?



The string conversion is rounding up the decimal places when I want it to show the exact number.







ios swift decimal nsnumberformatter nsdecimalnumber






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 8:13







AjinkyaSharma

















asked Nov 12 '18 at 7:03









AjinkyaSharmaAjinkyaSharma

414312




414312












  • Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

    – CRD
    Nov 12 '18 at 8:10











  • Possible duplicate of Is floating point math broken?

    – Gereon
    Nov 12 '18 at 9:31

















  • Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

    – CRD
    Nov 12 '18 at 8:10











  • Possible duplicate of Is floating point math broken?

    – Gereon
    Nov 12 '18 at 9:31
















Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

– CRD
Nov 12 '18 at 8:10





Can you edit your question and add the declaration, and inferred type if it's not explicit, of doubleValueOfDecimal?

– CRD
Nov 12 '18 at 8:10













Possible duplicate of Is floating point math broken?

– Gereon
Nov 12 '18 at 9:31





Possible duplicate of Is floating point math broken?

– Gereon
Nov 12 '18 at 9:31












2 Answers
2






active

oldest

votes


















0














Use wrapper method of NSNumber class. `




 print(numb.stringValue)



let ns = NumberFormatter.init()
ns.allowsFloats = true
ns.maximumFractionDigits = 18
ns.minimumFractionDigits = 18
ns.roundingMode = .floor
ns.numberStyle = .decimal
let numb = NSNumber.init(value: doubleValueOfDecimal)
print(numb)
let string = ns.string(from: numb)
print(numb.stringValue)



Below Output for 2.95699999999998 , 12.95699999999998 values.




Output



2.95699999999998



2.95699999999998



12.95699999999998



12.95699999999998






share|improve this answer























  • I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

    – AjinkyaSharma
    Nov 12 '18 at 7:45


















0














You are falling down the cracks between the expectations of the behaviour of decimal numbers and the reality that Float and Double are binary floating-point, that is the fractional part decimal numbers as sums of 1/10's, 1/100's etc. while for binary numbers it is sums of 1/2's, 1/4's etc. and some values exact in one are inexact in the other and vice-versa.



Change your code to include:



let doubleValueOfDecimal : Decimal = Decimal(string:"12.95699999999998")!
let numb = doubleValueOfDecimal as NSDecimalNumber


and the output is probably what you expect:



12.95699999999998
12.956999999999980000


The Decimal type is a decimal floating-point value type, NSDecimalNumber is a subclass of NSNumber which holds a Decimal value.



HTH



(Note: you have to initialise the Decimal from a string as the using a numeric literal appears to involve the Swift compiler using binary floating point at some point in the process...)






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257276%2fnumberformatter-stringfromnsnumber-method-has-issues-with-decimal-places%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Use wrapper method of NSNumber class. `




     print(numb.stringValue)



    let ns = NumberFormatter.init()
    ns.allowsFloats = true
    ns.maximumFractionDigits = 18
    ns.minimumFractionDigits = 18
    ns.roundingMode = .floor
    ns.numberStyle = .decimal
    let numb = NSNumber.init(value: doubleValueOfDecimal)
    print(numb)
    let string = ns.string(from: numb)
    print(numb.stringValue)



    Below Output for 2.95699999999998 , 12.95699999999998 values.




    Output



    2.95699999999998



    2.95699999999998



    12.95699999999998



    12.95699999999998






    share|improve this answer























    • I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

      – AjinkyaSharma
      Nov 12 '18 at 7:45















    0














    Use wrapper method of NSNumber class. `




     print(numb.stringValue)



    let ns = NumberFormatter.init()
    ns.allowsFloats = true
    ns.maximumFractionDigits = 18
    ns.minimumFractionDigits = 18
    ns.roundingMode = .floor
    ns.numberStyle = .decimal
    let numb = NSNumber.init(value: doubleValueOfDecimal)
    print(numb)
    let string = ns.string(from: numb)
    print(numb.stringValue)



    Below Output for 2.95699999999998 , 12.95699999999998 values.




    Output



    2.95699999999998



    2.95699999999998



    12.95699999999998



    12.95699999999998






    share|improve this answer























    • I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

      – AjinkyaSharma
      Nov 12 '18 at 7:45













    0












    0








    0







    Use wrapper method of NSNumber class. `




     print(numb.stringValue)



    let ns = NumberFormatter.init()
    ns.allowsFloats = true
    ns.maximumFractionDigits = 18
    ns.minimumFractionDigits = 18
    ns.roundingMode = .floor
    ns.numberStyle = .decimal
    let numb = NSNumber.init(value: doubleValueOfDecimal)
    print(numb)
    let string = ns.string(from: numb)
    print(numb.stringValue)



    Below Output for 2.95699999999998 , 12.95699999999998 values.




    Output



    2.95699999999998



    2.95699999999998



    12.95699999999998



    12.95699999999998






    share|improve this answer













    Use wrapper method of NSNumber class. `




     print(numb.stringValue)



    let ns = NumberFormatter.init()
    ns.allowsFloats = true
    ns.maximumFractionDigits = 18
    ns.minimumFractionDigits = 18
    ns.roundingMode = .floor
    ns.numberStyle = .decimal
    let numb = NSNumber.init(value: doubleValueOfDecimal)
    print(numb)
    let string = ns.string(from: numb)
    print(numb.stringValue)



    Below Output for 2.95699999999998 , 12.95699999999998 values.




    Output



    2.95699999999998



    2.95699999999998



    12.95699999999998



    12.95699999999998







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 7:29









    Pratik SodhaPratik Sodha

    537415




    537415












    • I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

      – AjinkyaSharma
      Nov 12 '18 at 7:45

















    • I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

      – AjinkyaSharma
      Nov 12 '18 at 7:45
















    I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

    – AjinkyaSharma
    Nov 12 '18 at 7:45





    I have used NumberFormatter class for a reason. The maximumFractionDigits and minimumFractionDigits are variables and not constants. So I can't ditch the whole setup for Formatter and directly use the stringValue

    – AjinkyaSharma
    Nov 12 '18 at 7:45













    0














    You are falling down the cracks between the expectations of the behaviour of decimal numbers and the reality that Float and Double are binary floating-point, that is the fractional part decimal numbers as sums of 1/10's, 1/100's etc. while for binary numbers it is sums of 1/2's, 1/4's etc. and some values exact in one are inexact in the other and vice-versa.



    Change your code to include:



    let doubleValueOfDecimal : Decimal = Decimal(string:"12.95699999999998")!
    let numb = doubleValueOfDecimal as NSDecimalNumber


    and the output is probably what you expect:



    12.95699999999998
    12.956999999999980000


    The Decimal type is a decimal floating-point value type, NSDecimalNumber is a subclass of NSNumber which holds a Decimal value.



    HTH



    (Note: you have to initialise the Decimal from a string as the using a numeric literal appears to involve the Swift compiler using binary floating point at some point in the process...)






    share|improve this answer



























      0














      You are falling down the cracks between the expectations of the behaviour of decimal numbers and the reality that Float and Double are binary floating-point, that is the fractional part decimal numbers as sums of 1/10's, 1/100's etc. while for binary numbers it is sums of 1/2's, 1/4's etc. and some values exact in one are inexact in the other and vice-versa.



      Change your code to include:



      let doubleValueOfDecimal : Decimal = Decimal(string:"12.95699999999998")!
      let numb = doubleValueOfDecimal as NSDecimalNumber


      and the output is probably what you expect:



      12.95699999999998
      12.956999999999980000


      The Decimal type is a decimal floating-point value type, NSDecimalNumber is a subclass of NSNumber which holds a Decimal value.



      HTH



      (Note: you have to initialise the Decimal from a string as the using a numeric literal appears to involve the Swift compiler using binary floating point at some point in the process...)






      share|improve this answer

























        0












        0








        0







        You are falling down the cracks between the expectations of the behaviour of decimal numbers and the reality that Float and Double are binary floating-point, that is the fractional part decimal numbers as sums of 1/10's, 1/100's etc. while for binary numbers it is sums of 1/2's, 1/4's etc. and some values exact in one are inexact in the other and vice-versa.



        Change your code to include:



        let doubleValueOfDecimal : Decimal = Decimal(string:"12.95699999999998")!
        let numb = doubleValueOfDecimal as NSDecimalNumber


        and the output is probably what you expect:



        12.95699999999998
        12.956999999999980000


        The Decimal type is a decimal floating-point value type, NSDecimalNumber is a subclass of NSNumber which holds a Decimal value.



        HTH



        (Note: you have to initialise the Decimal from a string as the using a numeric literal appears to involve the Swift compiler using binary floating point at some point in the process...)






        share|improve this answer













        You are falling down the cracks between the expectations of the behaviour of decimal numbers and the reality that Float and Double are binary floating-point, that is the fractional part decimal numbers as sums of 1/10's, 1/100's etc. while for binary numbers it is sums of 1/2's, 1/4's etc. and some values exact in one are inexact in the other and vice-versa.



        Change your code to include:



        let doubleValueOfDecimal : Decimal = Decimal(string:"12.95699999999998")!
        let numb = doubleValueOfDecimal as NSDecimalNumber


        and the output is probably what you expect:



        12.95699999999998
        12.956999999999980000


        The Decimal type is a decimal floating-point value type, NSDecimalNumber is a subclass of NSNumber which holds a Decimal value.



        HTH



        (Note: you have to initialise the Decimal from a string as the using a numeric literal appears to involve the Swift compiler using binary floating point at some point in the process...)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 9:26









        CRDCRD

        45k44870




        45k44870



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257276%2fnumberformatter-stringfromnsnumber-method-has-issues-with-decimal-places%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)