美文网首页
ios - 四舍五入字符串

ios - 四舍五入字符串

作者: biubiuboom | 来源:发表于2020-06-08 18:06 被阅读0次

/**
四舍五入字符串

@param round 小数位 eg: 2
@param numberString 数字 eg 0.125
@return 四舍五入之后的 eg: 0.13
*/

  • (double )roundNumberStringWithRound:(NSInteger)round numberString:(NSString *)numberString{

    if (numberString == nil) {
    return 0;
    }
    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:round raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *aDN = [[NSDecimalNumber alloc] initWithString:numberString];
    NSDecimalNumber *resultDN = [aDN decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    return resultDN.doubleValue;
    }

相关文章

网友评论

      本文标题:ios - 四舍五入字符串

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