萬以下的自動返回,我添加了 萬/百萬/千萬/億 的單位,可以根據(jù)自己的需求進行單位修改,
參考來源
+(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對象的創(chuàng)建 參數(shù) 1.RoundingMode 一個取舍枚舉值 2.scale 處理范圍 3.raiseOnExactness 精確出現(xiàn)異常是否拋出原因 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;
}