iOS Developer Beta 18

I want to ask about NSDecimalNumber. is it any changes for use this function ? i test use number like this.

example: a = "1000000.0" var a i make number formatter use NumberFormatter

b = NSDecimalNumber(string: a with number formatter).decimalValue

i try to print b. the value return 1. Anyone can help ?

NSDecimalNumber is surprisingly hard to use correctly. It’s also true that Foundation has been significantly reworked in iOS 18 beta, so you may be seeing fallout from that change. Given those two factoids, it’s hard to say whether this is your issue or Foundation’s.

You posted some snippets of code but not enough for me to offer an opinion as to what’s happening. Can you expand that into something I can run?

ps Please format your code using a code block (triple backquotes) so that it’s easier to read. See Quinn’s Top Ten DevForums Tips for this and other tips.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Oke thank u i will send the email

Hello,

I would like to inquire about a specific function and whether there have been any modifications made to it in the iOS 18 beta developer version. I have tested this function on iOS versions 17, 16, 15, 14, 13, and 12, and it has been functioning properly.

The function in question involves converting a decimal number, such as 12000000/12, into a string. The expected result on iOS 17 and earlier versions is 1000000. However, when I use iOS 18, the result is 1. Example code :

import Foundation

var computedAmount: Decimal = 12000000.0

computedAmount = computedAmount/12
computedAmount = NSDecimalNumber(string: computedAmount.formattedAmountIgnoreFraction).decimalValue
print("value of computet amount \(computedAmount)")

extension Decimal {
    var formattedAmountIgnoreFraction: String? {
        let formatter = NumberFormatter()
        formatter.numberStyle = .none
        formatter.usesGroupingSeparator = true
        formatter.generatesDecimalNumbers = true
        formatter.minimumFractionDigits = 0
        formatter.maximumFractionDigits = 0
        return formatter.string(from: self as NSDecimalNumber)
    }
}

I would appreciate any information or clarification regarding this change in behavior.

Thank you for your attention to this matter.

i will send the email

I can’t provide one-on-one technical support via email, but I’m happy to continue this discussion here.

To set the stage, Foundation is receiving major surgery in this year’s OS releases as part of the effort discussed in What’s next for Foundation. This work will produce compatibility problems, and it’s important to test your code on the latest betas and report any problems you see.

However, NSDecimalNumber is a tricky type so, before sending you down that path, I first wanna have a quick look at your test case. Thanks for posting that.

Your code is kinda strange, but I presume that that’s the result of your distilling this down into a test case. Running it on iOS 18.0b2 I see that formattedAmountIgnoreFraction is returning a string with group separators (commas in my case) and that’s causing NSDecimalNumber.init(string:) to return the wrong value. That seems to be a bug in NSDecimalNumber.init(string:). The documented makes it clear that it should be handling group separators.

So, you should definitely file a bug about that. Please post your bug number, just for the record.

As a temporary workaround, you could change formattedAmountIgnoreFraction to return a string without group separators (set usesGroupingSeparator to false), and then you’ll get the correct result.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

iOS Developer Beta 18
 
 
Q