iOS 数字转 xx 万 xx 亿

App 开发显示 XX 万 xx 亿

APP开发中界面显示很多地方需要显示数字,但是如果数字过大,可能会有将数字显示成 xx 万、xx 百万、xx 亿的需求,简单封装了一下,备忘,以便日后使用。

这是声明文件,调用者可以决定保留几位小数,是否需要四舍五入,也可依据代码自行扩展方法。

//  TGCaculateNumberTool.h
//  Tungee
//
//  Created by VanJay on 2018/8/22.
//  Copyright © 2018年 Tungee. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, TGCNRoundingMode) {
    TGCNRoundingModeUpAndDown = 0,  ///< 四舍五入
    TGCNRoundingModeOnlyUp,         ///< 只入不舍
    TGCNRoundingModeOnlyDown,       ///< 只舍不入
};

@interface TGCaculateNumberTool : NSObject
/**
 返回数字描述

 @param number 数字
 @param toFixed 保留小数位数
 @param roundingMode 是否四舍五入
 */
+ (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(TGCNRoundingMode)roundingMode;
@end

这是实现文件

//  TGCaculateNumberTool.m
//  Tungee
//
//  Created by VanJay on 2018/8/22.
//  Copyright © 2018年 Tungee. All rights reserved.
//

#import "TGCaculateNumberTool.h"

@implementation TGCaculateNumberTool

static NSNumberFormatter *formater;
static NSDictionary *configDict;

+ (void)initialize {
    if (self == [TGCaculateNumberTool class]) {
        formater = [[NSNumberFormatter alloc] init];
        // 可自行扩展
        configDict = @{
            @"wan": @{
                @"unit": @"万",
                @"dividend": @(10000.0)
            },
            @"yi": @{
                @"unit": @"亿",
                @"dividend": @(100000000.0)
            },
            @"wanYi": @{
                @"unit": @"万亿",
                @"dividend": @(1000000000000.0)
            }
        };
    }
}

+ (NSString *)stringFromNumber:(double)number toFixedCount:(NSInteger)toFixed roundingMode:(TGCNRoundingMode)roundingMode {
    NSString *output;

    formater.maximumFractionDigits = toFixed;

    if (roundingMode == TGCNRoundingModeUpAndDown) {
        formater.roundingMode = NSNumberFormatterRoundHalfEven;  // 四舍五入
    } else if (roundingMode == TGCNRoundingModeOnlyUp) {
        formater.roundingMode = NSNumberFormatterRoundCeiling;  // 只入不舍
    } else if (roundingMode == TGCNRoundingModeOnlyDown) {
        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)], configDict[@"wan"][@"unit"]];
    } else if (number <= 999999999999) {  // 999999999999
        double value = (double)number / [configDict[@"yi"][@"dividend"] doubleValue];
        output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], configDict[@"yi"][@"unit"]];
    } else {
        double value = (double)number / [configDict[@"wanYi"][@"dividend"] doubleValue];
        output = [NSString stringWithFormat:@"%@%@", [formater stringFromNumber:@(value)], configDict[@"wanYi"][@"unit"]];
    }
    return output;
}

/**
 计算特定小数位数

 @param input 输入数字
 @param toFixed 保留小数位数
 @param isRounded 是否四舍五入
 */
//+ (double)roundFloat:(double)input toFixedCount:(NSInteger)toFixed rounded:(BOOL)isRounded {
//
//    double scale = 1;
//    if (toFixed == 0) {
//        scale = 1;
//    } else if (toFixed == 1) {
//        scale = 10;
//    } else if (toFixed == 2) {
//        scale = 100;
//    } else if (toFixed == 3) {
//        scale = 1000;
//    } else if (toFixed == 4) {
//        scale = 10000;
//    }
//    if (isRounded) {
//        return (floorf(input * scale + 0.5)) / scale;
//    } else {
//        return (floorf(input * scale - 0.5)) / scale;
//    }
//}
@end