ChartCalculateUtils.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // CalculateUtils.m
  3. // ChartView
  4. //
  5. // Created by Simon Zhou on 14-6-20.
  6. // Copyright (c) 2014年 muchinfo. All rights reserved.
  7. //
  8. #import "ChartCalculateUtils.h"
  9. @implementation ChartCalculateUtils
  10. /**
  11. * @brief 取得小数位数
  12. *
  13. * @param number 目标数据
  14. *
  15. * @return 小数位数
  16. */
  17. + (NSInteger)getDecimalDigits:(NSDecimalNumber *)number
  18. {
  19. NSInteger returnValue = 0;
  20. NSString *tempString = [number stringValue];
  21. NSRange range = [tempString rangeOfString:@"."];
  22. if (range.location != NSNotFound) {
  23. returnValue = tempString.length - range.location -1;
  24. }
  25. return returnValue;
  26. }
  27. /**
  28. * @brief 截取指定小数位的值
  29. *
  30. * @param price 目标数据
  31. * @param position 有效小数位
  32. *
  33. * @return 截取后数据
  34. */
  35. + (NSString *)notRounding:(NSDecimalNumber *)price afterPoint:(NSInteger)position
  36. {
  37. NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
  38. //minUnit==0
  39. if (position == 0) {
  40. return [[price decimalNumberByRoundingAccordingToBehavior:behavior] description];
  41. }
  42. NSString *returnString = nil;
  43. NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
  44. NSDecimalNumber *roundedOunces = [price decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
  45. NSString *formatString = nil;
  46. switch (position) {
  47. case 1:
  48. formatString = @"%.1f";
  49. break;
  50. case 2:
  51. formatString = @"%.2f";
  52. break;
  53. case 3:
  54. formatString = @"%.3f";
  55. break;
  56. case 4:
  57. formatString = @"%.4f";
  58. break;
  59. case 5:
  60. formatString = @"%.5f";
  61. break;
  62. case 6:
  63. formatString = @"%.6f";
  64. break;
  65. case 7:
  66. formatString = @"%.7f";
  67. break;
  68. case 8:
  69. formatString = @"%.8f";
  70. break;
  71. default:
  72. formatString = @"%.2f";
  73. break;
  74. }
  75. returnString = [NSString stringWithFormat:formatString,[roundedOunces floatValue]];
  76. return returnString;
  77. }
  78. /**
  79. * @brief 截取指定小数位的值
  80. *
  81. * @param price 目标数据
  82. * @param position 有效小数位
  83. *
  84. * @return 截取后数据
  85. */
  86. + (nullable NSDecimalNumber *)notRoundingWithDecimalNumber:(nonnull NSDecimalNumber *)price afterPoint:(NSInteger)position
  87. {
  88. NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
  89. NSDecimalNumber *roundedOunces;
  90. roundedOunces = [price decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
  91. return roundedOunces;
  92. }
  93. /**
  94. * @brief 取绝对值的方法
  95. *
  96. * @param num 目标数据
  97. *
  98. * @return 绝对值
  99. */
  100. + (NSDecimalNumber *)abs:(NSDecimalNumber *)num
  101. {
  102. if ([num compare:[NSDecimalNumber zero]] == NSOrderedAscending) {
  103. // Number is negative. Multiply by -1
  104. NSDecimalNumber * negativeOne = [NSDecimalNumber decimalNumberWithMantissa:1
  105. exponent:0
  106. isNegative:YES];
  107. return [num decimalNumberByMultiplyingBy:negativeOne];
  108. } else {
  109. return num;
  110. }
  111. }
  112. /**
  113. * @brief 获取两个时间之间的分钟数
  114. *
  115. * @param fromDate 开始时间
  116. * @param toDate 结束时间
  117. *
  118. * @return 分钟数
  119. */
  120. + (NSInteger)diffMinuteWithFromDate:(nonnull NSDate *)fromDate toDate:(nonnull NSDate *)toDate {
  121. if (!fromDate)
  122. return 0;
  123. NSCalendar* chineseClendar = [NSCalendar currentCalendar];
  124. NSInteger unitFlags = NSCalendarUnitMinute;
  125. NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:fromDate toDate:toDate options:0];
  126. return [cps minute];
  127. }
  128. /**
  129. * @brief 获取指定时间的当前分钟0秒时间
  130. *
  131. * @param date 目标时间对象
  132. *
  133. * @return 当前分钟0秒时间
  134. */
  135. + (NSDate *)zeroSecondDateWithDate:(NSDate *)date
  136. {
  137. NSCalendar* clendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
  138. NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
  139. NSDateComponents *components = [clendar components:unitFlags fromDate:date];
  140. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  141. dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  142. 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]]];
  143. return returnDate;
  144. }
  145. @end