首页 > 代码库 > oc 小数乘法,并取整

oc 小数乘法,并取整

oc的double,float类型无法满足小数乘法的需求

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #00afca } span.s1 { }</style>

只能用NSDecimalNumber实现

    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];

 NSDecimalNumber *a = [[NSDecimalNumber alloc]initWithString:@"0.8"];
    NSDecimalNumber *b = [[NSDecimalNumber alloc]initWithString:@"0.6"];
//相乘并取整
    NSDecimalNumber *c = [a decimalNumberByMultiplyingBy:b withBehavior:roundingBehavior];

 

oc 小数乘法,并取整