123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // CaculateNumberTool.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/2/9.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "CaculateNumberTool.h"
- @implementation CaculateNumberTool
- static NSNumberFormatter *formater;
- static NSDictionary *configDict;
- + (void)initialize {
- if (self == [CaculateNumberTool class]) {
- formater = [[NSNumberFormatter alloc] init];
- // 可自行扩展
- configDict = @{
- @"qian": @{
- @"unit": kLocalizedString(@"千"),
- @"enUnit": @"k",
- @"dividend": @(1000.0)
- },
- @"wan": @{
- @"unit": kLocalizedString(@"万"),
- @"enUnit": @"w",
- @"dividend": @(10000.0)
- },
- @"yi": @{
- @"unit": kLocalizedString(@"亿"),
- @"enUnit": @"y",
- @"dividend": @(100000000.0)
- },
- @"wanYi": @{
- @"unit": kLocalizedString(@"万亿"),
- @"enUnit": @"wy",
- @"dividend": @(1000000000000.0)
- }
- };
- }
- }
- + (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode {
- return [CaculateNumberTool stringFromNumber:number toFixedCount:toFixed roundingMode:roundingMode unit:YES];
- }
- /**
- 返回数字描述
- @param number 数字
- @param toFixed 保留小数位数
- @param roundingMode 是否四舍五入
- @param unit 是否显示单位
- */
- + (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode unit:(BOOL)unit {
- NSString *output;
- formater.maximumFractionDigits = toFixed;
- if (roundingMode == CNRoundingModeUpAndDown) {
- formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
- } else if (roundingMode == CNRoundingModeOnlyUp) {
- formater.roundingMode = NSNumberFormatterRoundCeiling; // 只入不舍
- } else if (roundingMode == CNRoundingModeOnlyDown) {
- formater.roundingMode = NSNumberFormatterRoundFloor; // 只舍不入
- } else { // 默认
- formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
- }
- // 如果要五入恰好传入临界值,则h对该值加1,换单位,避免出现10000万这样情况
- if (formater.roundingMode == NSNumberFormatterRoundHalfEven || formater.roundingMode == NSNumberFormatterRoundCeiling) {
- if (number == 9999 || number == 99999999 || number == 999999999999) {
- number = number + 1;
- }
- }
- if (number <= 0) {
- output = @"0";
- } else if (number <= 9999) {
- output = [NSString stringWithFormat:@"%@", [formater stringFromNumber:@(number)]];
- } else if (number <= 99999999) { // 9999999
- double value = (double)number / [configDict[@"wan"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wan"][@"unit"]:@""];
- } else if (number <= 999999999999) { // 999999999999
- double value = (double)number / [configDict[@"yi"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"yi"][@"unit"]:@""];
- } else {
- double value = (double)number / [configDict[@"wanYi"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wanYi"][@"unit"]:@""];
- }
- return output;
- }
- /**
- 返回拼音单位的数字描述(k、w、y、yw)
- @param number 数字
- @param toFixed 保留小数位数
- @param roundingMode 是否四舍五入
- @param unit 是否显示单位
- */
- + (NSString *)stringFromPinyinNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(CNRoundingMode)roundingMode unit:(BOOL)unit {
- NSString *output;
- formater.maximumFractionDigits = toFixed;
- if (roundingMode == CNRoundingModeUpAndDown) {
- formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
- } else if (roundingMode == CNRoundingModeOnlyUp) {
- formater.roundingMode = NSNumberFormatterRoundCeiling; // 只入不舍
- } else if (roundingMode == CNRoundingModeOnlyDown) {
- formater.roundingMode = NSNumberFormatterRoundFloor; // 只舍不入
- } else { // 默认
- formater.roundingMode = NSNumberFormatterRoundHalfEven; // 四舍五入
- }
- // 如果要五入恰好传入临界值,则h对该值加1,换单位,避免出现10000万这样情况
- if (formater.roundingMode == NSNumberFormatterRoundHalfEven || formater.roundingMode == NSNumberFormatterRoundCeiling) {
- if (number == 9999 || number == 99999999 || number == 999999999999) {
- number = number + 1;
- }
- }
- if (number <= 0) {
- output = @"0";
- } else if (number <= 999) {
- output = [NSString stringWithFormat:@"%@", [formater stringFromNumber:@(number)]];
- } else if (number <= 9999) {
- double value = (double)number / [configDict[@"qian"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"qian"][@"enUnit"]:@""];
- } else if (number <= 99999999) { // 9999999
- double value = (double)number / [configDict[@"wan"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wan"][@"enUnit"]:@""];
- } else if (number <= 999999999999) { // 999999999999
- double value = (double)number / [configDict[@"yi"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"yi"][@"enUnit"]:@""];
- } else {
- double value = (double)number / [configDict[@"wanYi"][@"dividend"] doubleValue];
- output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], unit?configDict[@"wanYi"][@"enUnit"]:@""];
- }
- return output;
- }
- @end
|