保留小数点后两位 第三位大于零就进一

思想就是先降序 在升序

  • (NSString *)multiplying:(double)amount1 and:(double)amount2 afterPoint:(int)position {

    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

    NSDecimalNumber *decimal1 = [[NSDecimalNumber alloc] initWithDouble:amount1];
    NSDecimalNumber *decimal2 = [[NSDecimalNumber alloc] initWithDouble:amount2];

    NSDecimalNumberHandler *roundingBehavior1 = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:3 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

    NSDecimalNumber *result = [decimal1 decimalNumberByMultiplyingBy:decimal2 withBehavior:roundingBehavior1];

    NSDecimalNumber *decimal3 = [[NSDecimalNumber alloc] initWithDouble:1.0];

    NSDecimalNumber *result1 = [result decimalNumberByMultiplyingBy:decimal3 withBehavior:roundingBehavior];

    return [result1 stringValue];
    }

你可能感兴趣的:(保留小数点后两位 第三位大于零就进一)