Questions tagged [nsdecimalnumber]

NSDecimalNumber, an immutable subclass of NSNumber, provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.

NSDecimalNumber, an immutable subclass of NSNumber (), provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.

244 questions
7
votes
4 answers

How to split a NSDecimalnumber between decimal & integer & back

I can't figure how get the integral & decimal value from a NSDecimalnumber. For example, if I have 10,5, how get 10 & 5? And if I have 10 & 5, how return to 10,5? Mmm ok, that is looking better! However I have the concer about lossing decimals.…
mamcx
  • 15,916
  • 26
  • 101
  • 189
6
votes
2 answers

How can I format a SKProduct price into a Swift currency string?

If you have code such as this, then p will be "2.99": let price = 2.99 let p = String(format: "%.2f", price) However, if you have code like this: let priceNS: NSDecimalNumber = 2.99 let p2 = String(format: "%.2f", priceNS) Then p2 is "0.00". How…
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
6
votes
1 answer

NSDecimalNumber subtraction

I need to subtract 0.5 from number a and set the answer to number b. My code looks like it would work but I'm not sure what I'm doing wrong. The error I get Is on the subtraction line, the error says incompatible type for argument 1 of…
nosedive25
  • 2,477
  • 5
  • 30
  • 45
6
votes
3 answers

How to check for NSDecimalNumber.zero() in Swift

Using 'num1' as a comparison reference to determine if 'num2' or 'num3' are zero. Have done this successfully in Objective-C, and am trying to do it in Swift: let num1: NSDecimalNumber = NSDecimalNumber.zero() let num2: NSDecimalNumber =…
H_H
  • 605
  • 1
  • 9
  • 13
5
votes
2 answers

NSDecimalFormatter and currency symbols : $US instead of $

I must be missing something obvious but since I lost way too much time on this, I'll let the experts have a look. I'm just willing to get a string formatted exactly how I want it but the currency symbol is giving me an issue : it's using some kind…
omartin
  • 116
  • 1
  • 5
5
votes
2 answers

Swift: Decode imprecise decimal correctly

I need to decode (Decodable protocol) an imprecise decimal value correctly, from this question I understand how to properly handle the Decimal instantiation, but how can I do this when decoding? If trying to init any number as a String if let value…
Jose
  • 263
  • 4
  • 14
5
votes
1 answer

Why does NSDecimalNumber.notANumber.intValue return 9?

I found a bug in my code that is caused by NSDecimalNumber.notANumber.intValue returning 9, while I would expect NaN (as floatValue or doubleValue return). Does anybody know why?
phi
  • 10,634
  • 6
  • 53
  • 88
5
votes
2 answers

NSDecimalNumber question for core data and iPhone

I'm fairly new to core data and iphone programming. Maybe this is an obvious answer, so if anyone can point me to a tutorial or other resource, it's greatly appreciated! I have a core data entity that is a decimal type as it's dealing with…
JohnnyRedTruss
  • 259
  • 3
  • 11
5
votes
1 answer

Cocoa Decimals and Protocol Buffers, compatibility with .NET

I've been working with Protocol Buffers with an iOS app written using CocoaTouch and have run into what probably amounts to a rookie mistake with decimal numbers. Protocol Buffers does not have a native decimal type - see…
hocker
  • 688
  • 6
  • 18
5
votes
2 answers

NSDecimalNumber round long numbers

I'm trying to get NSDecimalNumber to print out large numbers, 15 or more digits. At 15 digits I see 111,111,111,111,111. Above 15 digits I see 1,111,111,111,111,110 even though the number being formatted is 1111111111111111. An example to…
user449521
  • 71
  • 1
  • 3
5
votes
1 answer

NSRoundBankers is not working as documented by Apple

So, according to Apple's documentation about NSRoundBankers: Round to the closest possible return value; when halfway between two possibilities, return the possibility whose last digit is even. While this is true for positive numbers, I am not…
lupidan
  • 573
  • 1
  • 7
  • 20
5
votes
1 answer

iOS 8 number conversion/formatting error? (cannot reproduce)

The following method takes input from a UITextField and formats it for display. This code has worked flawlessly for years, but a problem was just reported on the iPhone 6 Plus using iOS 8.1. It happens every time for the user but I have not been…
SAHM
  • 4,078
  • 7
  • 41
  • 77
5
votes
2 answers

NSDecimalNumber zeros following decimal point (iPhone SDK)

I have the following code (...) numberStyle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 …
Chris
  • 51
  • 1
  • 2
5
votes
1 answer

Comparing NSDecimalNumber won't work?

For some reason, the comparing logic isn't working correctly... it wont compare two NSDecimalNumber objects. Sometimes it works, sometimes it doesn't. Really weird. The if statement works on some compilations, and sometimes not. Is this the right…
James Heald
  • 841
  • 1
  • 12
  • 33
5
votes
2 answers

Conversion for NSString to NSDecimalNumber in Objective-C

What is the conversion for NSString to NSDecimalNumber? unsigned long long order; if (i==1) { order = [feeArray objectAtIndex:0]; } if (i==2) { order = [amountArray objectAtIndex:0]; }
user1501354
  • 151
  • 3
  • 10
1
2
3
16 17