NumberFormatter string(from:NSNumber) method has issues with decimal places
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
add a comment |
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
Can you edit your question and add the declaration, and inferred type if it's not explicit, ofdoubleValueOfDecimal
?
– CRD
Nov 12 '18 at 8:10
Possible duplicate of Is floating point math broken?
– Gereon
Nov 12 '18 at 9:31
add a comment |
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
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
ios swift decimal nsnumberformatter nsdecimalnumber
edited Nov 12 '18 at 8:13
AjinkyaSharma
asked Nov 12 '18 at 7:03
AjinkyaSharmaAjinkyaSharma
304211
304211
Can you edit your question and add the declaration, and inferred type if it's not explicit, ofdoubleValueOfDecimal
?
– CRD
Nov 12 '18 at 8:10
Possible duplicate of Is floating point math broken?
– Gereon
Nov 12 '18 at 9:31
add a comment |
Can you edit your question and add the declaration, and inferred type if it's not explicit, ofdoubleValueOfDecimal
?
– 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
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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...)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
answered Nov 12 '18 at 7:29
Pratik SodhaPratik Sodha
495311
495311
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
add a comment |
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
add a comment |
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...)
add a comment |
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...)
add a comment |
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...)
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...)
answered Nov 12 '18 at 9:26
CRDCRD
44.7k44870
44.7k44870
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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