首页 > 代码库 > Foundation框架 - NSNumber类
Foundation框架 - NSNumber类
NSNumber类
NSFormatter
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"1******************* NSFormatter ***********************");
NSNumber* nb1=[NSNumber numberWithInt:25];
NSNumber* nb2=[[NSNumber alloc]initWithBool:YES];
NSNumber* nb3=[NSNumber numberWithChar:255];
NSNumber* nb4 =[NSNumber numberWithUnsignedInteger:-1];
NSNumber* nb5=[NSNumber numberWithDouble:12.5];
NSNumber* nb6=[NSNumber numberWithFloat:12.89];
NSNumber* nb7=[NSNumber numberWithInteger:12];
NSNumber* nb8=[NSNumber numberWithLong:123123];
int n=[nb1 intValue];
//打印观察现象
NSLog(@"%d",n);
NSLog(@"int nb1=%@",nb1);
NSLog(@"BOOL nb2=%@",nb2);
NSLog(@"char nb3=%@",nb3);
NSLog(@"nb4=%@",nb4);
NSLog(@"nb5=%@",nb5);
NSLog(@"nb6=%@",nb6);
NSLog(@"nb7=%@",nb7);
NSLog(@"nb8=%@",nb8);
if (nb4.unsignedIntegerValue==NSUIntegerMax) {
NSLog(@"-1和[%ld]相等",NSUIntegerMax);
}
NSLog(@"nb4=[%@]",nb4);
NSComparisonResult result =[nb1 compare:nb2];
NSLog(@"%ld",result);
if (result==NSOrderedDescending) {
NSLog(@"nb1>nb2");
}else if(result==NSOrderedAscending){
NSLog(@"nb1<nb2");
}else{
NSLog(@"nb1=nb2");
}
NSNumberFormatter
NSLog(@"1******************* NSNumberFormatter *****************");
NSNumber* nbInt=[NSNumber numberWithInt:345678];
NSNumberFormatter* fmt=[[NSNumberFormatter alloc]init];
fmt.numberStyle=NSNumberFormatterDecimalStyle;
fmt.numberStyle=NSNumberFormatterCurrencyStyle;
fmt.numberStyle=NSNumberFormatterNoStyle; //无格式
NSString* formatted=[fmt stringFromNumber:nbInt];
NSLog(@"[%@]格式化后:[%@]",nbInt,formatted);
//产生一个浮点数
NSNumber* nbDouble=[NSNumber numberWithDouble:23.45678f];
fmt.numberStyle=NSNumberFormatterDecimalStyle;
NSString* fmtedDbl=[fmt stringFromNumber:nbDouble];
NSLog(@"[%@]格式化后:[%@]",nbDouble,formatted);
//设置小数四舍五入
fmt.roundingMode=NSNumberFormatterRoundFloor; //后面所有舍掉
fmt.roundingMode=NSNumberFormatterRoundHalfUp; //四舍五入
fmt.roundingMode=NSNumberFormatterRoundUp; //直接进位(全入)
fmt.roundingMode=NSNumberFormatterRoundDown; //
NSLog(@"[%@]格式化后:[%@]",nbDouble,fmtedDbl);
//设置小数的位数(默认是小数点三位)
//货币格式
fmt.numberStyle=NSNumberFormatterCurrencyStyle;
//通过currencyCode 能够指定货币代码(人民币:CNY)
fmt.currencyCode=@"USD";
//通过currencySymbol 能够指定货币符号
fmt.currencySymbol=@"HKD";
formatted= [fmt stringFromNumber:nbInt];
NSLog(@"[%@]格式化货币样式后:[%@]",nbInt,formatted);
}
return 0;
}
练习:随机输入一串阿拉伯数字,将其转换成大写金额形式。
#import <Foundation/Foundation.h>
void lowerToUpper(NSString* number);
int main(int argc, const char * argv[])
{
@autoreleasepool
{
lowerToUpper(@"56");
}
return 0;
}
//实现转换函数
void lowerToUpper(NSString* number)
{
//定义一个大写数组
NSArray* upper=@[@"零",@"壹",@"贰",@"弎",@"肆",
@"伍",@"陆",@"柒",@"捌",@"玖"];
NSString* result=@"";
for (int i=0; i<[number length]; i++) {
//NSUInteger location;
//NSUInteger length;
NSRange range={i,1};//指定位置和长度
//-substringWithRange: 依照所给出的位置,长度,从字符串中截取子串
NSString* str=[number substringWithRange:range];
int value=http://www.mamicode.com/[str intValue];"hljs-built_in">NSLog(@"转换后的结果为: %@",result);
}
Foundation框架 - NSNumber类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。