CaculateNumberTool.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // CaculateNumberTool.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/2/9.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "CaculateNumberTool.h"
  9. @implementation CaculateNumberTool
  10. static NSNumberFormatter *formater;
  11. static NSDictionary *configDict;
  12. + (void)initialize {
  13. if (self == [CaculateNumberTool class]) {
  14. formater = [[NSNumberFormatter alloc] init];
  15. // 可自行扩展
  16. configDict = @{
  17. @"qian": @{
  18. @"unit": kLocalizedString(@"千"),
  19. @"enUnit": @"k",
  20. @"dividend": @(1000.0)
  21. },
  22. @"wan": @{
  23. @"unit": kLocalizedString(@"万"),
  24. @"enUnit": @"w",
  25. @"dividend": @(10000.0)
  26. },
  27. @"yi": @{
  28. @"unit": kLocalizedString(@"亿"),
  29. @"enUnit": @"y",
  30. @"dividend": @(100000000.0)
  31. },
  32. @"wanYi": @{
  33. @"unit": kLocalizedString(@"万亿"),
  34. @"enUnit": @"wy",
  35. @"dividend": @(1000000000000.0)
  36. }
  37. };
  38. }
  39. }
  40. + (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode {
  41. return [CaculateNumberTool stringFromNumber:number toFixedCount:toFixed roundingMode:roundingMode unit:YES];
  42. }
  43. /**
  44. 返回数字描述
  45. @param number 数字
  46. @param toFixed 保留小数位数
  47. @param roundingMode 是否四舍五入
  48. @param unit 是否显示单位
  49. */
  50. + (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode unit:(BOOL)unit {
  51. NSString *output;
  52. formater.maximumFractionDigits = toFixed;
  53. if (roundingMode == CNRoundingModeUpAndDown) {
  54. formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
  55. } else if (roundingMode == CNRoundingModeOnlyUp) {
  56. formater.roundingMode = NSNumberFormatterRoundCeiling; // 只入不舍
  57. } else if (roundingMode == CNRoundingModeOnlyDown) {
  58. formater.roundingMode = NSNumberFormatterRoundFloor; // 只舍不入
  59. } else { // 默认
  60. formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
  61. }
  62. // 如果要五入恰好传入临界值,则h对该值加1,换单位,避免出现10000万这样情况
  63. if (formater.roundingMode == NSNumberFormatterRoundHalfEven || formater.roundingMode == NSNumberFormatterRoundCeiling) {
  64. if (number == 9999 || number == 99999999 || number == 999999999999) {
  65. number = number + 1;
  66. }
  67. }
  68. if (number <= 0) {
  69. output = @"0";
  70. } else if (number <= 9999) {
  71. output = [NSString stringWithFormat:@"%@", [formater stringFromNumber:@(number)]];
  72. } else if (number <= 99999999) { // 9999999
  73. double value = (double)number / [configDict[@"wan"][@"dividend"] doubleValue];
  74. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wan"][@"unit"]:@""];
  75. } else if (number <= 999999999999) { // 999999999999
  76. double value = (double)number / [configDict[@"yi"][@"dividend"] doubleValue];
  77. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"yi"][@"unit"]:@""];
  78. } else {
  79. double value = (double)number / [configDict[@"wanYi"][@"dividend"] doubleValue];
  80. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wanYi"][@"unit"]:@""];
  81. }
  82. return output;
  83. }
  84. /**
  85. 返回拼音单位的数字描述(k、w、y、yw)
  86. @param number 数字
  87. @param toFixed 保留小数位数
  88. @param roundingMode 是否四舍五入
  89. @param unit 是否显示单位
  90. */
  91. + (NSString *)stringFromPinyinNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode unit:(BOOL)unit {
  92. NSString *output;
  93. formater.maximumFractionDigits = toFixed;
  94. if (roundingMode == CNRoundingModeUpAndDown) {
  95. formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
  96. } else if (roundingMode == CNRoundingModeOnlyUp) {
  97. formater.roundingMode = NSNumberFormatterRoundCeiling; // 只入不舍
  98. } else if (roundingMode == CNRoundingModeOnlyDown) {
  99. formater.roundingMode = NSNumberFormatterRoundFloor; // 只舍不入
  100. } else { // 默认
  101. formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
  102. }
  103. // 如果要五入恰好传入临界值,则h对该值加1,换单位,避免出现10000万这样情况
  104. if (formater.roundingMode == NSNumberFormatterRoundHalfEven || formater.roundingMode == NSNumberFormatterRoundCeiling) {
  105. if (number == 9999 || number == 99999999 || number == 999999999999) {
  106. number = number + 1;
  107. }
  108. }
  109. if (number <= 0) {
  110. output = @"0";
  111. } else if (number <= 999) {
  112. output = [NSString stringWithFormat:@"%@", [formater stringFromNumber:@(number)]];
  113. } else if (number <= 9999) {
  114. double value = (double)number / [configDict[@"qian"][@"dividend"] doubleValue];
  115. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"qian"][@"enUnit"]:@""];
  116. } else if (number <= 99999999) { // 9999999
  117. double value = (double)number / [configDict[@"wan"][@"dividend"] doubleValue];
  118. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wan"][@"enUnit"]:@""];
  119. } else if (number <= 999999999999) { // 999999999999
  120. double value = (double)number / [configDict[@"yi"][@"dividend"] doubleValue];
  121. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"yi"][@"enUnit"]:@""];
  122. } else {
  123. double value = (double)number / [configDict[@"wanYi"][@"dividend"] doubleValue];
  124. output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wanYi"][@"enUnit"]:@""];
  125. }
  126. return output;
  127. }
  128. @end