#pragma mark ----两个数相加-----------
+(NSString *)calculateByadding:(NSString *)number1 secondNumber:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSDecimalNumber *addingNum = [num1 decimalNumberByAdding:num2];
return [addingNum stringValue];
}
#pragma mark ----两个数相减------------ number1 - number2
+(NSString *)calculateBySubtractingMinuend:(NSString *)number1 subtractorNumber:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSDecimalNumber *addingNum = [num1 decimalNumberBySubtracting:num2];
return [addingNum stringValue];
}
#pragma mark ----两个数相乘------------
+(NSString *)calculateByMultiplying:(NSString *)number1 secondNumber:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSDecimalNumber *multiplyingNum = [num1 decimalNumberByMultiplyingBy:num2];
return [multiplyingNum stringValue];
}
#pragma mark ----两个数相除------------
+ (NSString *)calculateByDividingNumber:(NSString *)number1 secondNumber:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSDecimalNumber *dividingNum = [num1 decimalNumberByDividingBy:num2];
return [dividingNum stringValue];
}
#pragma mark ----四舍五入------------
+ (NSString *)calculateTargetNumber:(NSString *)targetNumber ByRounding:(NSUInteger)scale
{
NSDecimalNumberHandler * handler = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundPlain scale:scale raiseOnExactness:NO raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:targetNumber];
NSDecimalNumber *roundingNum = [num1 decimalNumberByRoundingAccordingToBehavior:handler];
return [roundingNum stringValue];
}
#pragma mark ----是否相等------------
+ (BOOL)calculateIsEqualNumber:(NSString *)number1 secondNumber:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSComparisonResult result = [num1 compare:num2];
if (result == NSOrderedSame) {
return YES;
}
return NO;
}
#pragma mark ----是否大于------------
+ (BOOL)calculateNumber: (NSString *)number1 IsGreaterThan:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSComparisonResult result = [num1 compare:num2];
if (result == NSOrderedDescending) {
return YES;
}
return NO;
}
#pragma mark ----是否小于------------
+ (BOOL)calculateNumber:(NSString *)number1 IsLessThan:(NSString *)number2
{
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:number1];
NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithString:number2];
NSComparisonResult result = [num1 compare:num2];
if (result == NSOrderedAscending) {
return YES;
}
return NO;
}
#pragma mark --10指数运算--------
+(NSString *)calculateTargetNumber:(NSString *)number1 ByRonding:(short)power
{
NSDecimalNumber * balanceNumber = [[NSDecimalNumber alloc]initWithString:number1];
NSDecimalNumber * ehNumber = [balanceNumber decimalNumberByMultiplyingByPowerOf10:-power];
return [ehNumber stringValue];
}
#pragma mark ---------单纯保留小数位数----------
+(NSString *)calculateRetainedDecimalNumber:(NSString *)targetNumber ByRonding:(short)power
{
NSDecimalNumber * balanceNumber = [[NSDecimalNumber alloc]initWithString:targetNumber];
NSDecimalNumberHandler * hander = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:4 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSNumber *changeNumber = [balanceNumber decimalNumberByRoundingAccordingToBehavior:hander];
return [changeNumber stringValue];
}
swift 加减乘除
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...