| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- //
- // CalculateUtils.m
- // ChartView
- //
- // Created by Simon Zhou on 14-6-20.
- // Copyright (c) 2014年 muchinfo. All rights reserved.
- //
- #import "ChartCalculateUtils.h"
- @implementation ChartCalculateUtils
- /**
- * @brief 取得小数位数
- *
- * @param number 目标数据
- *
- * @return 小数位数
- */
- + (NSInteger)getDecimalDigits:(NSDecimalNumber *)number
- {
- NSInteger returnValue = 0;
-
- NSString *tempString = [number stringValue];
- NSRange range = [tempString rangeOfString:@"."];
-
- if (range.location != NSNotFound) {
- returnValue = tempString.length - range.location -1;
- }
-
- return returnValue;
- }
- /**
- * @brief 截取指定小数位的值
- *
- * @param price 目标数据
- * @param position 有效小数位
- *
- * @return 截取后数据
- */
- + (NSString *)notRounding:(NSDecimalNumber *)price afterPoint:(NSInteger)position
- {
- NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
-
- //minUnit==0
- if (position == 0) {
- return [[price decimalNumberByRoundingAccordingToBehavior:behavior] description];
- }
-
- NSString *returnString = nil;
- NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
- NSDecimalNumber *roundedOunces = [price decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
- NSString *formatString = nil;
- switch (position) {
- case 1:
- formatString = @"%.1f";
- break;
- case 2:
- formatString = @"%.2f";
- break;
- case 3:
- formatString = @"%.3f";
- break;
- case 4:
- formatString = @"%.4f";
- break;
- case 5:
- formatString = @"%.5f";
- break;
- case 6:
- formatString = @"%.6f";
- break;
- case 7:
- formatString = @"%.7f";
- break;
- case 8:
- formatString = @"%.8f";
- break;
- default:
- formatString = @"%.2f";
- break;
- }
- returnString = [NSString stringWithFormat:formatString,[roundedOunces floatValue]];
-
- return returnString;
- }
- /**
- * @brief 截取指定小数位的值
- *
- * @param price 目标数据
- * @param position 有效小数位
- *
- * @return 截取后数据
- */
- + (nullable NSDecimalNumber *)notRoundingWithDecimalNumber:(nonnull NSDecimalNumber *)price afterPoint:(NSInteger)position
- {
- NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
- NSDecimalNumber *roundedOunces;
-
- roundedOunces = [price decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
-
- return roundedOunces;
- }
- /**
- * @brief 取绝对值的方法
- *
- * @param num 目标数据
- *
- * @return 绝对值
- */
- + (NSDecimalNumber *)abs:(NSDecimalNumber *)num
- {
- if ([num compare:[NSDecimalNumber zero]] == NSOrderedAscending) {
- // Number is negative. Multiply by -1
- NSDecimalNumber * negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1
- exponent:0
- isNegative:YES];
- return [num decimalNumberByMultiplyingBy:negativeOne];
- } else {
- return num;
- }
- }
- /**
- * @brief 获取两个时间之间的分钟数
- *
- * @param fromDate 开始时间
- * @param toDate 结束时间
- *
- * @return 分钟数
- */
- + (NSInteger)diffMinuteWithFromDate:(nonnull NSDate *)fromDate toDate:(nonnull NSDate *)toDate {
- if (!fromDate)
- return 0;
-
- NSCalendar* chineseClendar = [NSCalendar currentCalendar];
- NSInteger unitFlags = NSCalendarUnitMinute;
-
- NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:fromDate toDate:toDate options:0];
-
- return [cps minute];
- }
- /**
- * @brief 获取指定时间的当前分钟0秒时间
- *
- * @param date 目标时间对象
- *
- * @return 当前分钟0秒时间
- */
- + (NSDate *)zeroSecondDateWithDate:(NSDate *)date
- {
- NSCalendar* clendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
- NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
- NSDateComponents *components = [clendar components:unitFlags fromDate:date];
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
-
- NSDate *returnDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d %d:%d:00",(int)[components year],(int)[components month],(int)[components day],(int)[components hour],(int)[components minute]]];
-
- return returnDate;
- }
- @end
|