//
//  CalculateHelper.m
//  XFFruit
//
//  Created by Z on 16/7/14.
//  Copyright © 2016年 Xummer. All rights reserved.
//

#import "CalculateHelper.h"

@implementation CalculateHelper


+ (NSDecimalNumber *)calculateNum1:(id)num1 num2:(id)num2 type:(CalculateType)type roundingType:(NSRoundingMode)roundingType cutLenth:(NSInteger)coutLenth {
    NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
                                       
                                       decimalNumberHandlerWithRoundingMode:roundingType
                                       
                                       scale:coutLenth
                                       
                                       raiseOnExactness:NO
                                       
                                       raiseOnOverflow:NO
                                       
                                       raiseOnUnderflow:NO
                                       
                                       raiseOnDivideByZero:YES];
    NSDecimalNumber *decimalNum1 = [self changeType:num1];
    NSDecimalNumber *decimalNum2 = [self changeType:num2];
    
    if (decimalNum1 == nil || decimalNum2 == nil) {
        return @"传入的类型不正确";
    }
    
    NSDecimalNumber *decimalResult;
    NSString *result;
    switch (type) {
        case CalculateTypeAdd: {
            decimalResult = [decimalNum1 decimalNumberByAdding:decimalNum2 withBehavior:roundUp];
            result = [decimalResult stringValue];
            break;
        }
        case CalculateTypeSub: {
            decimalResult = [decimalNum1 decimalNumberBySubtracting:decimalNum2 withBehavior:roundUp];
            result = [decimalResult stringValue];
            break;
        }
        case CalculateTypeMul: {
            decimalResult = [decimalNum1 decimalNumberByMultiplyingBy:decimalNum2 withBehavior:roundUp];
            result = [decimalResult stringValue];
            break;
        }
        case CalculateTypeDiv: {
            decimalResult = [decimalNum1 decimalNumberByDividingBy:decimalNum2 withBehavior:roundUp];
            result = [decimalResult stringValue];
            break;
        }
    }
    
    
    return decimalResult;
}

+ (NSDecimalNumber *)changeType:(id)num1 {
    NSDecimalNumber *decimalNum1;
    if ([num1 isKindOfClass:[NSDecimalNumber class]]) {
        decimalNum1 = num1;
    }else if([num1 isKindOfClass:[NSString class]]){
        decimalNum1 = [NSDecimalNumber decimalNumberWithString:num1];
    }else if ([num1 isKindOfClass:[NSNumber class]]){
        decimalNum1 = [NSDecimalNumber decimalNumberWithDecimal:[num1 decimalValue]];
    }else{
        return nil;
    }
    return decimalNum1;
}


+ (NSString *)doubleValueToMoneyString:(double)value {
    return nil;
}

@end
