万以下的自动返回,我添加了 万/百万/千万/亿 的单位,可以根据自己的需求进行单位修改,
参考来源
+(NSString *)getDealNumwithstring:(NSString *)string{
NSDecimalNumber *numberA = [NSDecimalNumber decimalNumberWithString:string];
NSDecimalNumber *numberB ;
NSString *unitStr;
if (string.length > 3 && string.length <7 ) {
numberB = [NSDecimalNumber decimalNumberWithString:@"10000"];
unitStr = @"万";
}else if (string.length ==7){
numberB = [NSDecimalNumber decimalNumberWithString:@"1000000"];
unitStr = @"百万";
}else if(string.length ==8){
numberB = [NSDecimalNumber decimalNumberWithString:@"10000000"];
unitStr = @"千万";
}
else if (string.length > 8){
numberB = [NSDecimalNumber decimalNumberWithString:@"100000000"];
unitStr = @"亿";
}else{
return string;
}
//NSDecimalNumberBehaviors对象的创建 参数 1.RoundingMode 一个取舍枚举值 2.scale 处理范围 3.raiseOnExactness 精确出现异常是否抛出原因 4.raiseOnOverflow 上溢出是否抛出原因 4.raiseOnUnderflow 下溢出是否抛出原因 5.raiseOnDivideByZero 除以0是否抛出原因。
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
/// 这里不仅包含Multiply还有加 减 乘。
NSDecimalNumber *numResult = [numberA decimalNumberByDividingBy:numberB withBehavior:roundingBehavior];
NSString *strResult = [NSString stringWithFormat:@"%@%@",[numResult stringValue],unitStr];
return strResult;
}