iOS-金額小寫轉大寫

來源:互聯網
上載者:User

標籤:

一.目的

1. 金額小寫轉化成大寫. 如 123456.65 --> 壹拾貳萬三仟肆佰伍拾陸元陸角伍分

2. 只能處理13位元的金額,並且只能處理到小數點後兩位.

二.代碼

 #import "ViewController.h"

 

#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define ScreenHeight [UIScreen mainScreen].bounds.size.height

 

@interface ViewController ()

 

@property (nonatomic, strong) UILabel   * smallLabel;  //顯示小寫金額

@property (nonatomic, strong) UILabel   * bigLabel;     //顯示大寫金額

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

    [self initUI];

}

 

- (void)initUI

{

    NSString * money = @"123456.65";

    

    self.smallLabel = [[UILabel alloc] init];

    self.smallLabel.frame = CGRectMake(10, 30, ScreenWidth - 20, 40);

    self.smallLabel.text = money;

    self.smallLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:self.smallLabel];

    

    //大寫

    self.bigLabel = [[UILabel alloc] init];

    self.bigLabel.frame = CGRectMake(10, 100, ScreenWidth - 20, 40);

    self.bigLabel.text = [self changetoBigMoney:money];

    self.bigLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:self.bigLabel];

}

 

//可以直接拷貝該封裝的方法 

#pragma mark 金幣大小寫 --> 封裝的方法

- (NSString *)changetoBigMoney:(NSString *)numstr

{

    //轉化成double類型

    double numberals = [numstr doubleValue];

    

    NSArray *numberchar = @[@"零",@"壹",@"貳",@"三",@"肆",@"伍",@"陸",@"柒",@"捌",@"玖"];

    NSArray *inunitchar = @[@"",@"拾",@"佰",@"仟"];

    NSArray *unitname = @[@"",@"萬",@"億",@"萬億"];

    

    //金額乘以100轉換成字串(去除圓角分數值)

    NSString *valstr=[NSString stringWithFormat:@"%.2f",numberals];

    NSLog(@"valstr: %@",valstr);

    

    NSString *prefix;

    NSString *suffix;

    

    NSLog(@"%lu",(unsigned long)valstr.length);

    

    if (valstr.length <= 2)

    {

        [email protected]"零元";

        

        if (valstr.length == 0)

        {

            [email protected]"零角零分";

        }

        else if (valstr.length == 1)

        {

            suffix=[NSString stringWithFormat:@"%@分",[numberchar objectAtIndex:[valstr intValue]]];

        }

        else

        {

            NSString *head = [valstr substringToIndex:1];

            NSString *foot = [valstr substringFromIndex:1];

            suffix=[NSString stringWithFormat:@"%@角%@分",[numberchar objectAtIndex:[head intValue]],[numberchar objectAtIndex:[foot intValue]]];

        }

    }

    else

    {

        [email protected]"";

        [email protected]"";

        

        int flag = (int)valstr.length - 2;

        NSLog(@"flag: %d",flag);

        

        NSString *head = [valstr substringToIndex:flag-1];

        NSLog(@"head: %@",head);

        

        NSString *foot = [valstr substringFromIndex:flag];

        NSLog(@"foot: %@",foot);

        

        if (head.length>13)

        {

            return @"數值太大(最大支援13位整數),無法處理";

        }

        //處理整數部分

        NSMutableArray * ch = [[NSMutableArray alloc]init];

        for (int i = 0; i < head.length; i++)

        {

            NSLog(@"head[i]: %hu",[head characterAtIndex:i]);

            

            NSString * str=[NSString stringWithFormat:@"%x",[head characterAtIndex:i]-‘0‘];

            [ch addObject:str];

            

            NSLog(@"ch: %@",ch);

        }

        NSLog(@"ch_All: %@",ch);

 

        

        int zeronum = 0;

        

        NSLog(@"ch.count: %ld",ch.count);

        for (int i = 0; i < ch.count; i++)

        {

            int index = (ch.count - i - 1) % 4;//取段內位置

            NSLog(@"index: %d",index);

            

            int indexloc = (int)(ch.count - i - 1) / 4;//取段位置

            NSLog(@"indexloc: %d",indexloc);

            

            

            NSLog(@"ch[i]: %@",[ch objectAtIndex:i]);

            if ([[ch objectAtIndex:i] isEqualToString:@"0"])

            {

                zeronum++;

            }

            else

            {

                if (zeronum != 0)

                {

                    if (index != 3)

                    {

                        prefix=[prefix stringByAppendingString:@"零"];

                    }

                    zeronum = 0;

                }

                prefix = [prefix stringByAppendingString:[numberchar objectAtIndex:[[ch objectAtIndex:i]intValue]]];

                prefix = [prefix stringByAppendingString:[inunitchar objectAtIndex:index]];

            }

            if (index == 0 && zeronum < 4)

            {

                prefix=[prefix stringByAppendingString:[unitname objectAtIndex:indexloc]];

            }

        }

        prefix = [prefix stringByAppendingString:@"元"];

        //處理小數位

        if ([foot isEqualToString:@"00"])

        {

            suffix =[suffix stringByAppendingString:@"整"];

        }

        else if ([foot hasPrefix:@"0"])

        {

            NSString * footch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:1]-‘0‘];

            suffix = [NSString stringWithFormat:@"%@分",[numberchar objectAtIndex:[footch intValue]]];

        }

        else

        {

            NSString * headch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:0]-‘0‘];

            NSString * footch=[NSString stringWithFormat:@"%x",[foot characterAtIndex:1]-‘0‘];

            suffix = [NSString stringWithFormat:@"%@角%@分",[numberchar objectAtIndex:[headch intValue]],[numberchar objectAtIndex:[footch intValue]]];

        }

    }

    return [prefix stringByAppendingString:suffix];

}

 

@end

iOS-金額小寫轉大寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.