大写金额换算器iOS版源代码
人民币金额大写转换器输入数字即可转换成对应的人民币大写金额,操作非常简单,需一键点击,即可复制。是财务办公人员必备的小工具。
银行、单位和个人填写的各种票据和结算凭证是办理支付结算和现金收付的重要依据,直接关系到支付结算的准确、及时和安全。票据和结算凭证是银行、单位和个人凭以记载账务的会计凭证,是记载经济业务和明确经济责任的一种书面证明。因此,填写票据和结算凭证必须做到标准化、规范化、要素齐全、数字正确、字迹清晰、不错漏、不潦草、防止涂改。
中文大写金额数字应用正楷或行书填写,如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样,不得用一、二(两)、三、四、五、六、七、八、九、十、毛、另(或0)填写,不得自造简化字。如果金额数字书写中使用繁体字,如贰、陆、亿、万、圆的,也应受理。
以下是实现的主要代码:完整工程代码见http://www.mingjianhua.com/post/%E5%A4%A7%E5%86%99%E9%87%91%E9%A2%9D%E6%8D%A2%E7%AE%97%E5%99%A8iOS%E7%89%88%E6%BA%90%E4%BB%A3%E7%A0%81.html
本软件已在Appstore上架,可下载预览。
https://itunes.apple.com/us/app/da-xie-ren-min-bi-jin-e-zhuan/id932475713?l=zh&ls=1&mt=8
//
// ViewController.m
// numbers
//
// Created by MingJianhua on 14/10/14.
// Copyright (c) 2014年 MingJianhua. All rights reserved.
//
#import "ViewController.h"
#include <stdio.h>
#include <stdlib.h>
#import "config.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize txtNumber, txtViewChinese;
@synthesize btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
@synthesize btn_Dot, btn_Back, btn_Clear, btn_Copy;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
txtNumber.delegate = self;
//txtChinese.adjustsFontSizeToFitWidth = YES;
[self SetButtonBorder];
txtNumber.layer.borderWidth = 1.0;
txtNumber.layer.cornerRadius = 5;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
[txtNumber.layer setBorderColor:colorref];//边框颜色
txtViewChinese.layer.borderWidth = 1.0;
txtViewChinese.layer.cornerRadius = 5;
//CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
[txtViewChinese.layer setBorderColor:colorref];//边框颜色
strArray1 = [NSArray arrayWithObjects:@"", @"拾", @"佰", @"仟", nil];
strArray2 = [NSArray arrayWithObjects:@"", @"萬", @"亿", nil];
strArray3 = [NSArray arrayWithObjects:@"角", @"分", nil];
strArray4 = [NSArray arrayWithObjects:@"零", @"壹", @"贰", @"叁", @"肆", @"伍", @"陆", @"柒", @"捌", @"玖", nil];
adView = [[AdMoGoView alloc]
initWithAppKey:@"8f9f18594cb841c6aac520c862ee4d8b"
adType:AdViewTypeNormalBanner
adMoGoViewDelegate:self];
adView.adWebBrowswerDelegate = self;
adView.frame = CGRectMake(0, _MainScreen_Height - 50 +
(IOS7_OR_LATER?20:0), _MainScreen_Width, 50);
[self.view addSubview:adView];
}
- (void)viewWillAppear:(BOOL)animated
{
int nCount = 0;
NSNumber *nUsedCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"UsedCount"];
if (nUsedCount != nil)
{
nCount = [nUsedCount intValue];
}
if (nCount==3 )
{
altPingfen = [[UIAlertView alloc] initWithTitle:@"提示" message:@"这是您第3次用打开了本应用了,如果觉得好用,就去打个分鼓励下吧。" delegate:self cancelButtonTitle:@"残忍拒绝" otherButtonTitles:@"去给好评", nil];
[altPingfen show];
nCount ++;
nUsedCount = [NSNumber numberWithInt:nCount];
}
else if(nCount<3)
{
nCount ++;
nUsedCount = [NSNumber numberWithInt:nCount];
}
[[NSUserDefaults standardUserDefaults] setObject:nUsedCount forKey:@"UsedCount"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction) NumberPress:(id)sender
{
UIButton *btnNumber = (UIButton *)sender;
int number = (int)btnNumber.tag - 100;
if (m_nCurrentNumber>=100000000000 && btndotpress == NO)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入数据太大了" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];
[alertView show];
return;
}
if(btndotpress==NO)
//判断是否按下了小数点。
{
m_nCurrentNumber = m_nCurrentNumber * 10 +
number;
}
else {
if (ii>=2)
{
return;
}
++ii;
m_nCurrentNumber = m_nCurrentNumber +
number * pow(0.1,ii); //i为点击小数点后再每输入一个数都退一个百分位。
}
if (btndotpress == NO)
txtNumber.text =
[NSString stringWithFormat:@"%lld",
(long long)m_nCurrentNumber];
else
txtNumber.text =
[NSString stringWithFormat:@"%.2f", m_nCurrentNumber];
//txtChinese.text = txtNumber.text;
//整数部分
NSString *sChinese;
sChinese = [self ConvertIntergerPart:m_nCurrentNumber];
sChinese = [sChinese stringByAppendingString: [self ConvertFractionalPart:m_nCurrentNumber]];
txtViewChinese.text =
sChinese;
//小数部分
//[NSArray with:@"元",@"拾",@"佰",nil];
//while (nIntNumber>10) {
// nIntNumber%10
//}
}
- (NSString *)ConvertIntergerPart:(double)
currentNumber
{
long long nIntNumber
= (long long)currentNumber;
NSString *sIntNumber = @"";
if (nIntNumber>0)
{
int nPos = 0;
int nIndex1 = 0,
nIndex2 = 0;
int nLastNumber = 0;
while(nIntNumber>0)
{
int nNumber = nIntNumber%10;
NSString *sNumber = [strArray4 objectAtIndex:nNumber];
if (nIndex2 >= 0 &&
nIndex1 == 0) {
sIntNumber = [[strArray2 objectAtIndex:nIndex2] stringByAppendingString:sIntNumber];
}
if (nNumber >0)
{
sIntNumber = [[strArray1 objectAtIndex:nIndex1] stringByAppendingString:sIntNumber];//[strArray4
objectAtIndex:nNumber]];
}
if (!(nNumber == 0 &&
(nIndex1==0 || nLastNumber==0 )))
{
sIntNumber = [sNumber stringByAppendingString:sIntNumber];//[strArray4
objectAtIndex:nNumber]];
}
nIntNumber = nIntNumber/10;
nLastNumber = nNumber;
nPos++;
nIndex1 = nPos%4;
nIndex2 = (nPos/4)%3;
if (nPos>8 &&
nIndex2==0) {
nIndex2++;
}
}
sIntNumber = [sIntNumber stringByAppendingString:@"元"];
}
return sIntNumber;
}
- (NSString *)ConvertFractionalPart:(double)
currentNumber
{
NSString *sFloatNumber = @"";
long long nIntNumber
= (long long)currentNumber;
float fFloatNumber = currentNumber - nIntNumber +0.001;
int nJiao = (int)(fFloatNumber*10);
int nFen = (int)(fFloatNumber*100)%10;
if (nJiao == 0 &&
nFen == 0) {
sFloatNumber = @"整";
}
else if (nJiao
== 0)
{
sFloatNumber = [sFloatNumber stringByAppendingString:@"零"];
sFloatNumber = [sFloatNumber stringByAppendingString:[strArray4 objectAtIndex:nFen]];
sFloatNumber = [sFloatNumber stringByAppendingString:@"分"];
}
else
{
sFloatNumber = [sFloatNumber stringByAppendingString:[strArray4 objectAtIndex:nJiao]];
sFloatNumber = [sFloatNumber stringByAppendingString:@"角"];
sFloatNumber = [sFloatNumber stringByAppendingString:[strArray4 objectAtIndex:nFen]];
sFloatNumber = [sFloatNumber stringByAppendingString:@"分"];
}
return sFloatNumber;
}
-(IBAction) DotPress:(id)sender
{
btndotpress = YES;
}
-(IBAction) BackPress:(id)sender
{
if(btndotpress == NO)
{
m_nCurrentNumber = (long long)m_nCurrentNumber/10;
}
else
{
if (ii == 0)
{
m_nCurrentNumber = m_nCurrentNumber/10;
btndotpress = NO;
}
else if(ii == 1)
{
m_nCurrentNumber = (long long)m_nCurrentNumber;
btndotpress = NO;
ii--;
}
else if(ii == 2)
{
long long nIntNumber
= (long long)m_nCurrentNumber;
double fFloatNumber = m_nCurrentNumber -
nIntNumber +0.001;
int nJiao = (int)(fFloatNumber*10);
m_nCurrentNumber = (double)nIntNumber
+ (double)nJiao/10;
ii--;
}
}
if (btndotpress == NO)
txtNumber.text =
[NSString stringWithFormat:@"%lld",
(long long)m_nCurrentNumber];
else
txtNumber.text =
[NSString stringWithFormat:@"%.2f", m_nCurrentNumber];
NSString *sChinese;
sChinese = [self ConvertIntergerPart:m_nCurrentNumber];
sChinese = [sChinese stringByAppendingString: [self ConvertFractionalPart:m_nCurrentNumber]];
txtViewChinese.text =
sChinese;
}
-(IBAction) ClearPress:(id)sender
{
m_nCurrentNumber = 0;
txtNumber.text = @"0";
btndotpress = NO;
//txtChinese.text = @"";
txtViewChinese.text = @"";
ii = 0;
}
-(IBAction) CopyPress:(id)sender
{
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = txtViewChinese.text;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"大写数字已复制到剪贴板" delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了", nil];
[alertView show];
}
-(void) SetButtonBorder
{
//int nWidth = _MainScreen_Width/3;
for (int i
= 0; i<=13; i++) {
int tag = 100 +i;
UIButton * btn = (UIButton *)[self.view viewWithTag:tag];
[btn.layer setMasksToBounds:YES];
[btn.layer setCornerRadius:0.0]; //设置矩圆角半径
[btn.layer setBorderWidth:0.5]; //边框宽度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
[btn.layer setBorderColor:colorref];//边框颜色
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
[textField resignFirstResponder];
textField.text = @"";
return NO;
}
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
//[txtChinese resignFirstResponder];
[txtNumber resignFirstResponder];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == altPingfen)
{
if (buttonIndex == 0)
{
}
else if (buttonIndex
== 1)
{
// 打分
NSString *str = [NSString stringWithFormat:
@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%d",
932475713 ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
else
{
}
altPingfen = nil;
}
}
/** * 广告开始请求回调 */
- (void)adMoGoDidStartAd:(AdMoGoView *)adMoGoView{ NSLog(@"广告开始请求回调");
}
/**
* 广告接收成功回调
*/
- (void)adMoGoDidReceiveAd:(AdMoGoView *)adMoGoView{ NSLog(@"广告接收成功回调");
}
/**
* 广告接收失败回调
*/
- (void)adMoGoDidFailToReceiveAd:(AdMoGoView *)adMoGoViewdidFailWithError:(NSError *)error{ NSLog(@"广告接收失败回调");
}
/**
* 点击广告回调
*/
- (void)adMoGoClickAd:(AdMoGoView *)adMoGoView{ NSLog(@"点击广告回调");
}
/** *自定义尺寸是否需要轮换 */
- (BOOL)adMoGoCustomSizeAdRotateOrNot{
return NO;//[YES/NO 是否需要轮换];
}
/**
*自定义广告尺寸
*/
- (CGSize)adMoGoCustomSize{
CGSize size;
size.width = _MainScreen_Width;
size.height = 50;
return size;
}
/**
*You can get notified when the user delete the ad 广告关闭回调
*/
- (void)adMoGoDeleteAd:(AdMoGoView *)adMoGoView{ NSLog(@"广告关闭回调");}
- (UIViewController *)viewControllerForPresentingModalView{
return self;
}
@end