How to use NSDecimalNumber?

前端 未结 4 565
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 00:51

I\'m building a app that needs to perform calculations on money.

I wonder how to properly use NSDecimalNumber, especially how to initialize it from integers, floats

4条回答
  •  春和景丽
    2020-11-30 00:56

    The correct way is actually to do this:

    NSDecimalNumber *floatDecimal = [[[NSDecimalNumber alloc] initWithFloat:42.13f] autorelease];
    NSDecimalNumber *doubleDecimal = [[[NSDecimalNumber alloc] initWithDouble:53.1234] autorelease];
    NSDecimalNumber *intDecimal = [[[NSDecimalNumber alloc] initWithInt:53] autorelease];
    
    NSLog(@"floatDecimal floatValue=%6.3f", [floatDecimal floatValue]);
    NSLog(@"doubleDecimal doubleValue=%6.3f", [doubleDecimal doubleValue]); 
    NSLog(@"intDecimal intValue=%d", [intDecimal intValue]);
    

    See more info here.

提交回复
热议问题