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
4
votes
2 answers

wrong decimal separator with NSDecimalNumber in iOS

I tried to output the description of a decimal number with the correct decimal separator in the following way: NSString* strValue = @"9.94300"; NSDecimalNumber* decimalNumber = [NSDecimalNumber decimalNumberWithString: strValue]; NSLocale*…
Waterman
  • 131
  • 3
  • 11
4
votes
1 answer

Get NSDecimalNumber with 2 decimal places

I want to create NSDecimalNumber after dividing 100. I am using following code: NSDecimalNumber *amountToSendNumber = [number decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:@"100"]]; It gives me correct value if I provide input…
iAsh
  • 447
  • 6
  • 19
4
votes
1 answer

When is it better to use an NSDecimal, NSDecimalNumber instead of a double?

For simple uses, such as tracking weight values like 65.1kg, is there any benefit of going with NSDecimal/NSDecimalNumber over double? My understanding here is double (or even float) provides more than enough precision in such cases. Please correct…
Gaurav Sharma
  • 2,680
  • 3
  • 26
  • 36
4
votes
1 answer

Casting currency values from JSON to NSDecimalNumber

I get the following JSON response from an API. { "status": "success", "data": [ { "actual_price": 30, "offered_deal_price": 16, "pending_balance": 12.8 } ] } All those values are prices. Which means they can be round…
Isuru
  • 30,617
  • 60
  • 187
  • 303
4
votes
2 answers

NSDecimalNumber multiplication strangeness

ExclusivePrice, quantity are both NSDecimalNumbers. NSDecimalNumber *price = [exclusivePrice decimalNumberByMultiplyingBy:quantity]; NSLog(@"%@ * %@ = %@", exclusivePrice, quantity, price); The result I get: 2010-04-05 00:22:29.111…
rein
  • 32,967
  • 23
  • 82
  • 106
4
votes
3 answers

Swift Exceptions to Exception handling

After perusing through forums and Swift documentation (not completely, I admit), it appears that instead of try-catch mechanisms, in Swift we are encouraged to write code that is more safe from exceptions. In light of that, I have a question about…
H_H
  • 605
  • 1
  • 9
  • 13
4
votes
3 answers

Set NSDecimal to zero

What are the different ways of setting a NSDecimal to zero? I'm not keen on any of the 3 I found so far. NSDecimal zero = {0}; // Not obvious what's going on here. NSDecimal zero = @(0).decimalValue; // @(0) appears to always return the same…
hpique
  • 119,096
  • 131
  • 338
  • 476
4
votes
2 answers

can we convert the NSDecimal values to UIImage?

i am sending some data and UIImage(converting UIImage to NSData) sending to server, My client is giving back the response what we send. But, Here my client giving the UIImage like the below format…
Babul
  • 1,268
  • 2
  • 15
  • 42
4
votes
1 answer

Why does the NSDecimalNumber 10^-65 squared result in 10^126 instead of resulting in an Underflow Exception?

So here's the code: NSDecimalNumber *test=[[NSDecimalNumber alloc] initWithInt:65]; number3=[[NSDecimalNumber one] decimalNumberByDividingBy:[[[NSDecimalNumber alloc] initWithInt:10]…
4
votes
7 answers

Check if NSDecimalNumber is whole number

What's the easiest way to check if an instance of NSDecimalNumber is a whole number? Thanks!
James
  • 762
  • 8
  • 22
4
votes
2 answers

NSDecimalNumber to the power of NSDecimalNumber

I have two NSDecimalNumbers and I need to apply one to the power of the other, originally this code was using doubles and I could compute this with the pow() function like this: double result = pow(value1, value2); The problem I have is I am…
Rob Gill
  • 327
  • 1
  • 10
3
votes
1 answer

How to format huge NSDecimal to string?

Got a big NSDecimal with high precision. Like this: NSString *decStr = @"999999999999.999999999999"; NSDecimal dec; NSScanner *scanner = [[NSScanner alloc] initWithString:decStr]; [scanner scanDecimal:&dec]; NSDecimalNumber *decNum =…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
3
votes
0 answers

NSPredicate for calculated field with NSDecimalNumber

I want to filter a calculated field (binary expression in terms of Apple documentation) in Core Data with a NSDecimalNumber value. The entity has two attributes: field1 and field2, both of type Decimal The predicate is: NSDecimalNumber *number =…
3
votes
1 answer

NSDecimalNumber(x).intValue returns -2, 0, 15 and 199, depending on the amount of decimals in x (x = 199.999...5)

We found an interesting case in our business logic that totally breaks our logic and we don't understand why NSDecimalNumber and Decimal behaves the way it does. My playground for the cases is as follows: import Foundation let pQuantity =…
Simon
  • 470
  • 1
  • 7
  • 22
3
votes
2 answers

Swift's Decimal precision issue

According to the docs here, Swift 3/4 Decimal type is a representation in base 10 bridged to NSDecimalNumber. However I'm having precision issues that do not reproduce when using NSDecimalNumber. let dec24 = Decimal(integerLiteral: 24) let dec1 =…
Rafael Nobre
  • 5,062
  • 40
  • 40
1 2
3
16 17