A little confused about NSDecimalNumber?












-1















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"













share|improve this question

























  • 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
















-1















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"













share|improve this question

























  • 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














-1












-1








-1








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"













share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















2














fin is ok but NSLog calls doubleValue. In earlier versions of macOS, NSLog did call description which returns "78.99". Solution:
NSLog(@"%@", fin.description).






share|improve this answer
























  • 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 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











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%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









2














fin is ok but NSLog calls doubleValue. In earlier versions of macOS, NSLog did call description which returns "78.99". Solution:
NSLog(@"%@", fin.description).






share|improve this answer
























  • 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 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
















2














fin is ok but NSLog calls doubleValue. In earlier versions of macOS, NSLog did call description which returns "78.99". Solution:
NSLog(@"%@", fin.description).






share|improve this answer
























  • 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 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














2












2








2







fin is ok but NSLog calls doubleValue. In earlier versions of macOS, NSLog did call description which returns "78.99". Solution:
NSLog(@"%@", fin.description).






share|improve this answer













fin is ok but NSLog calls doubleValue. In earlier versions of macOS, NSLog did call description which returns "78.99". Solution:
NSLog(@"%@", fin.description).







share|improve this answer












share|improve this answer



share|improve this answer










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 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



















  • 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 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

















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


















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%2f53299681%2fa-little-confused-about-nsdecimalnumber%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python