截取字符串

2019-07-17  本文已影响0人  edison0428
/**
 NSString 转 double 精度

 @param textString 字符串
 @param scale 小数点后保留的位数
 @return double
 */
+ (NSString *)StringChangeToDoubleForJingdu:(NSString *)textString Scale:(short)scale
{
    if(!textString || textString.length==0){
        return @"0";
    }
    
    NSDecimalNumber *aDN = [[NSDecimalNumber alloc] initWithString:textString];

    //scale: 小数点后保留的位数 RoundingMode: 小数保留的类型
    //Exactness:精度异常、Overflow:向上溢出、Underflow:向下溢出、DivideByZero:除数为0。当参数为YES出错会抛出异常,为NO时忽略异常。返回nil.
    NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown
                                                                                                      scale:scale
                                                                                           raiseOnExactness:NO
                                                                                            raiseOnOverflow:NO
                                                                                           raiseOnUnderflow:NO
                                                                                        raiseOnDivideByZero:NO];
    
    NSDecimalNumber *resultDN = [aDN decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

    return [resultDN stringValue];
    
    
}
上一篇下一篇

猜你喜欢

热点阅读