A little confused about NSDecimalNumber?
there are a short code
NSString *numString = @"2128.123123";
NSDecimalNumber *large = [NSDecimalNumber decimalNumberWithString:numString];
NSDecimalNumberHandler *decimalHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *fin = [large decimalNumberByRoundingAccordingToBehavior:decimalHandler];
NSLog(@"%@",fin);
seem ok, print "2128.12".
.....
but you can try numString = @"78.991";
NSLog(@"%@",fin)
print "78.98999999999999"....
why scale is invalid? expect "78.99"
objective-c double nsdecimalnumber cgfloat
|
show 1 more comment
there are a short code
NSString *numString = @"2128.123123";
NSDecimalNumber *large = [NSDecimalNumber decimalNumberWithString:numString];
NSDecimalNumberHandler *decimalHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *fin = [large decimalNumberByRoundingAccordingToBehavior:decimalHandler];
NSLog(@"%@",fin);
seem ok, print "2128.12".
.....
but you can try numString = @"78.991";
NSLog(@"%@",fin)
print "78.98999999999999"....
why scale is invalid? expect "78.99"
objective-c double nsdecimalnumber cgfloat
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
@luk2302 you're missing the point ofNSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."
– Willeke
Nov 14 '18 at 13:02
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15
|
show 1 more comment
there are a short code
NSString *numString = @"2128.123123";
NSDecimalNumber *large = [NSDecimalNumber decimalNumberWithString:numString];
NSDecimalNumberHandler *decimalHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *fin = [large decimalNumberByRoundingAccordingToBehavior:decimalHandler];
NSLog(@"%@",fin);
seem ok, print "2128.12".
.....
but you can try numString = @"78.991";
NSLog(@"%@",fin)
print "78.98999999999999"....
why scale is invalid? expect "78.99"
objective-c double nsdecimalnumber cgfloat
there are a short code
NSString *numString = @"2128.123123";
NSDecimalNumber *large = [NSDecimalNumber decimalNumberWithString:numString];
NSDecimalNumberHandler *decimalHandler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *fin = [large decimalNumberByRoundingAccordingToBehavior:decimalHandler];
NSLog(@"%@",fin);
seem ok, print "2128.12".
.....
but you can try numString = @"78.991";
NSLog(@"%@",fin)
print "78.98999999999999"....
why scale is invalid? expect "78.99"
objective-c double nsdecimalnumber cgfloat
objective-c double nsdecimalnumber cgfloat
edited Nov 15 '18 at 7:58
Sunson_alone
asked Nov 14 '18 at 11:55
Sunson_aloneSunson_alone
35
35
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
@luk2302 you're missing the point ofNSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."
– Willeke
Nov 14 '18 at 13:02
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15
|
show 1 more comment
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
@luk2302 you're missing the point ofNSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."
– Willeke
Nov 14 '18 at 13:02
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
@luk2302 you're missing the point of
NSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."– Willeke
Nov 14 '18 at 13:02
@luk2302 you're missing the point of
NSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."– Willeke
Nov 14 '18 at 13:02
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15
|
show 1 more comment
1 Answer
1
active
oldest
votes
fin
is ok but NSLog
calls doubleValue
. In earlier versions of macOS, NSLog
did call description
which returns "78.99". Solution:
NSLog(@"%@", fin.description)
.
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same asNSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. TryNSLog(@"%0.24g", fin.doubleValue)
.
– Willeke
Nov 15 '18 at 16:06
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%2f53299681%2fa-little-confused-about-nsdecimalnumber%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
fin
is ok but NSLog
calls doubleValue
. In earlier versions of macOS, NSLog
did call description
which returns "78.99". Solution:
NSLog(@"%@", fin.description)
.
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same asNSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. TryNSLog(@"%0.24g", fin.doubleValue)
.
– Willeke
Nov 15 '18 at 16:06
add a comment |
fin
is ok but NSLog
calls doubleValue
. In earlier versions of macOS, NSLog
did call description
which returns "78.99". Solution:
NSLog(@"%@", fin.description)
.
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same asNSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. TryNSLog(@"%0.24g", fin.doubleValue)
.
– Willeke
Nov 15 '18 at 16:06
add a comment |
fin
is ok but NSLog
calls doubleValue
. In earlier versions of macOS, NSLog
did call description
which returns "78.99". Solution:
NSLog(@"%@", fin.description)
.
fin
is ok but NSLog
calls doubleValue
. In earlier versions of macOS, NSLog
did call description
which returns "78.99". Solution:
NSLog(@"%@", fin.description)
.
answered Nov 14 '18 at 13:02
WillekeWilleke
7,75121024
7,75121024
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same asNSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. TryNSLog(@"%0.24g", fin.doubleValue)
.
– Willeke
Nov 15 '18 at 16:06
add a comment |
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same asNSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. TryNSLog(@"%0.24g", fin.doubleValue)
.
– Willeke
Nov 15 '18 at 16:06
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
you mean, the fin value is "78.99"(although the initial value is "78.991")? NSLog makes it wrong?
– Sunson_alone
Nov 15 '18 at 5:29
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
and why "2128.123123" worked ok ,"78.991" not correct?╮( ̄▽ ̄"")╭
– Sunson_alone
Nov 15 '18 at 5:39
NSLog(@"%@",fin)
does the same as NSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. Try NSLog(@"%0.24g", fin.doubleValue)
.– Willeke
Nov 15 '18 at 16:06
NSLog(@"%@",fin)
does the same as NSLog(@"%0.16g", fin.doubleValue)
. This is the broken floating point math problem. Try NSLog(@"%0.24g", fin.doubleValue)
.– Willeke
Nov 15 '18 at 16:06
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%2f53299681%2fa-little-confused-about-nsdecimalnumber%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
Possible duplicate of Is floating point math broken?
– luk2302
Nov 14 '18 at 11:56
Works for me. I think we need an MCVE: stackoverflow.com/help/mcve
– Ture Pålsson
Nov 14 '18 at 12:15
@luk2302 you're missing the point of
NSDecimalNumber
. "An object for representing and performing arithmetic on base-10 numbers."– Willeke
Nov 14 '18 at 13:02
@TurePålsson which version of macOS are you using?
– Willeke
Nov 14 '18 at 13:04
@Willeke I tested on Mojave, but I'm getting some linker warnings about stub files being out of sync (???) so it's possible that my system is somehow falling back to an older library...
– Ture Pålsson
Nov 14 '18 at 13:15