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
46
votes
6 answers

Swift 3 : Decimal to Int

I tried to convert Decimal to Int with the follow code: Int(pow(Decimal(size), 2) - 1) But I get: .swift:254:43: Cannot invoke initializer for type 'Int' with an argument list of type '(Decimal)' Here I know pow is returning a Decimal but it…
Colin Wang
  • 6,778
  • 5
  • 26
  • 42
43
votes
2 answers

Convert NSNumber to NSDecimalNumber

I realize that NSDecimalNumber inherits from NSNumber. However, I am struggling to figure out how I can init an NSDecimalNumber from an NSNumber or how to convert an NSNumber to an NSDecimalNumber. I have tried NSDecimalNumber* d = [aNumber…
Michael Rivers
  • 920
  • 2
  • 10
  • 17
21
votes
5 answers

How do I add NSDecimalNumbers?

OK this may be the dumb question of the day, but supposing I have a class with : NSDecimalNumber *numOne = [NSDecimalNumber numberWithFloat:1.0]; NSDecimalNumber *numTwo = [NSDecimalNumber numberWithFloat:2.0]; NSDecimalNumber *numThree =…
Terry B
  • 211
  • 1
  • 2
  • 3
16
votes
4 answers

Different ways of comparing NSDecimalNumber

For example, with primitive, I'll do this if ( (x >= 6000) && (x <= 20000) ) // do something here and with NSDecimalNumber, this is what I have if ( (([x compare:[NSNumber numberWithInt:6000]] == NSOrderedSame) || ([x…
X Slash
  • 4,133
  • 4
  • 27
  • 35
16
votes
5 answers

Converting NSDecimalNumber to NSString

I'm retrieving a key from an object that looks like this: po obj { TypeID = 3; TypeName = Asset; } The key value is being retrieved like this: NSString *typeId = (NSString*)[obj objectForKey:@"TypeID"]; Rather than typeId being an NSString, it…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
13
votes
3 answers

Convert String to NSDecimalNumber

I try to convert a String into a NSDecimalNumber here it my code: class func stringToDecimal (dataToDecimal: String) -> NSDecimalNumber { let dataToDecimal = dataToDecimal.stringByReplacingOccurrencesOfString(",", withString: ".") …
HongKongTom
  • 207
  • 1
  • 2
  • 11
12
votes
2 answers

Java BigDecimal in Swift

What data type I can use for parsing Java BigDecimal? In Objective - C it can be done by using NSDecimalNumber. Do I have Swift-native solution (without using NSDecimalNumber)?
Srj0x0
  • 458
  • 3
  • 12
12
votes
3 answers

How to round an NSDecimalNumber in swift?

I can't find any resources on this, and I've been trying all sorts of stuff, but nothing works. According to Apple's documentation, you round an NSDecimalNumber like this: NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior:…
Naldhelaan
  • 390
  • 4
  • 13
11
votes
3 answers

Turn a NSDecimalNumber negative

I am looking for a way to turn a NSDecimalNumber negative by multiplying by -1. /* decNumber is the one I would like to turn negative */ NSDecimalNumber *decNumber = [values objectAtIndex:billIndex]; NSDecimalNumber *minusOne = [[NSDecimalNumber…
Macarse
  • 91,829
  • 44
  • 175
  • 230
11
votes
1 answer

How to store 1.66 in NSDecimalNumber

I know float or double are not good for storing decimal number like money and quantity. I'm trying to use NSDecimalNumber instead. Here is my code in Swift playground. let number:NSDecimalNumber = 1.66 let text:String = String(describing:…
Joey
  • 115
  • 5
10
votes
4 answers

{NSDecimalNumber integerValue} behaving strangely in iOS8

OK team, this is weird. [NSDecimalNumber integerValue] is behaving strangely. I'm sat at a breakpoint, trying to figure out why some parts of my app are broken in iOS8, and I'm looking at a variable called "timeSeconds". It appears like this in the…
Matthew
  • 1,363
  • 11
  • 21
10
votes
1 answer

NSDecimalNumber decimalNumberWithString: ignores current locale

According to the documentation, [NSDecimalNumber decimalNumberWithString:] should use the locale decimal separator: Whether the NSDecimalSeparator is a period (as is used, for example, in the United States) or a comma (as is used, for example, in…
thejaz
  • 2,763
  • 2
  • 26
  • 40
8
votes
4 answers

Convert NSDecimalNumber to String

How can I convert NSDecimalNumber values to String? // Return type is NSDecimalNumber var price = prod.minimumPrice // 10 // need a String cell.priceLB.text = NSString(format:"%f", prod.minimumPrice) as String
Yatish Agrawal
  • 452
  • 5
  • 15
7
votes
1 answer

What locale argument to pass to NSDecimalNumber +decimalNumberWithString:locale: so it always works with NSString's using the dot (.) decimal mark?

I have an NSString which I want to convert into an NSDecimalNumber. The string is received from a server and is always formatted using the en_US locale like XXX.YYY and not like XXX,YYY. I want to create an NSDecimalNumber which accepts XXX.YYY…
Dave
  • 12,408
  • 12
  • 64
  • 67
7
votes
5 answers

Fractional Part of NSDecimalNumber

I'm using NSDecimalNumber to store a value for currency. I'm trying to write a method called "cents" which returns the decimal portion of the number as an NSString with a leading 0 if the number is < 10. So basically NSDecimalNumber *n =…
JamesB41
  • 703
  • 10
  • 20
1
2 3
16 17