/**
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];
}
网友评论