美文网首页
保留小数点后两位 第三位大于零就进一

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

作者: 高乔人 | 来源:发表于2018-07-26 19:48 被阅读10次

    思想就是先降序 在升序

    • (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];
      }

    相关文章

      网友评论

          本文标题:保留小数点后两位 第三位大于零就进一

          本文链接:https://www.haomeiwen.com/subject/lvhimftx.html