Search code examples
objective-cxcodensstringnsdecimalnumber

Not seeing the decimal from a string when converted into NSDecimalNumer


I can't see to find the answer to this, but I have a string that has a decimal point in it, and when I try to convert it to a NSDecimalNumber I only get back the whole number, not the decimal or what would come after it. This is what I am trying:

someText.text = @"200.00";
tempAmountOwed = [NSDecimalNumber decimalNumberWithString:someText.text]; // gives me back 200

I can't seem to figure out if the decimalNumberWithString method is stripping out my decimal and ignoring what comes after it.

Thanks for the help!


Solution

  • You can use the method decimalNumberWithString: locale: method.

    for eg:-

    The code:

    NSLog(@"%@", [NSDecimalNumber decimalNumberWithString:@"200.00"]);
    NSLog(@"%@", [NSDecimalNumber decimalNumberWithString:@"200.00" locale:NSLocale.currentLocale]);
    

    Gives following log:

    200
    200.00
    

    Hope this Helps!