美文网首页
swift 加减乘除

swift 加减乘除

作者: 风吹吹 | 来源:发表于2022-08-09 17:34 被阅读0次
#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教程(2)

    swift的运算 加减乘除 三目运算符 与或关系 运算比较

  • swift 加减乘除

  • Swift4 基础学习

    注释 打印 基础运算符 常见的加减乘除Swift当然也要有 Tip:来丰富下英文词汇

  • swift NSDecimalNumber链式加减乘除算法

    用swift写的链式运算法则 附代码 备注: 如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到...

  • Swift基本运算符

    赋值运算符 数值运算 和大多数语言一样,swift也支持加减乘除四则运算,以及秋雨(%)运算 自增(自减)运算 复...

  • Swift1-运算符

    Swift的运算符与C++和其他语言的运算符大同小异,基础的诸如加减乘除逻辑运算为运算等等一应具全,而且包含了?:...

  • 第二十四节 Swift 高级运算符

    除了加减乘除运算符,常用的位运算符在 Swift里属于高级运算符。 其它的高级运算符还有移位运算符和溢出加法运算符...

  • Mircosoft Excel选择性粘贴运算加加减乘除

    Mircosoft Excel选择性粘贴运算加加减乘除的操作如下: 假设A列是需要进行加减乘除的数字,分别加减乘除...

  • CH02_02

    加减乘除

  • 奇奇怪怪的成语

    嘿,你知道“加减乘除”吗?“加减乘除”四个字,怕是无人不知,无人不晓了吧。但是你知道“加减乘除”居然是一个成语...

网友评论

      本文标题:swift 加减乘除

      本文链接:https://www.haomeiwen.com/subject/twbnwrtx.html