NSString *tempA = @"123";

NSString *tempB = @"456";

1,字符串拼接

NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

2,字符转int

int intString = [newString intValue];

3,int转字符

NSString *stringInt = [NSString stringWithFormat:@"%d",intString];

4,字符转float

float floatString = [newString floatValue];

5,float转字符

NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];

四舍五入问题

-(NSString *)notRounding:(float)price afterPoint:(int)position{

NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

NSDecimalNumber *ouncesDecimal;

NSDecimalNumber *roundedOunces;

ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];

roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

[ouncesDecimal release];

return [NSString stringWithFormat:@"%@",roundedOunces];

}

介绍一下参数:

price:需要处理的数字,

position:保留小数点第几位,

然后调用

float s =0.126;

NSString *sb = [self notRounding:s afterPoint:2];

NSLog(@"sb = %@",sb);

输出结果为:sb = 0.12

接下来介绍NSDecimalNumberHandler初始化时的关键参数:decimalNumberHandlerWithRoundingMode:NSRoundDown,

NSRoundDown代表的就是 只舍不入。

scale的参数position代表保留小数点后几位。

Objective-C中NSString与int和float的相互转换的更多相关文章

  1. object-c中NSString与int和float的相互转换

    1,字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2,字符转int in ...

  2. NSString与int和float的相互转换

    NSString *tempA = @"123"; NSString *tempB = @"456"; 1,字符串拼接 NSString *newString ...

  3. Objective - C 中NSString (字符串)与C中的字符串转换问题

    NSString是一个常用的类,NSString是原生支持unicode C中的字符串 比如char * a = "hello world";  是utf8类型的, char* d ...

  4. Objective - C中属性和点语法的使用

    一.属性        属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性         ...

  5. OC中NSString 的常用方法

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...

  6. objective C中的字符串NSStirng常用操作

    objective C中的字符串操作 在OC中创建字符串时,一般不使用C的方法,因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,在Cocoa中NSString集成的一些方法,可以很方便的 ...

  7. 理解Objective C 中id

    什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...

  8. 别在int与float上栽跟头(转)

    源:http://www.cnblogs.com/luguo3000/p/3719651.html int与float是我们每天编程都用的两种类型,但是我们真的足够了解它们吗.昨天在博客园看到一个比较 ...

  9. 【C++】C++中int与string的相互转换

    一.int转string 1.c++11标准增加了全局函数std::to_string: string to_string (int val); string to_string (long val) ...

随机推荐

  1. UVALive 6886 Golf Bot FFT

    Golf Bot 题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129724 Description Do ...

  2. 简单的文件上传html+ashx

    前台页面:<form action="upload.ashx" method="post" enctype="multipart/form-da ...

  3. saga中的saga(A Saga on Sagas)

    此文翻译自msdn,侵删. 原文地址:https://msdn.microsoft.com/en-us/library/jj591569.aspx Process Managers, Coordina ...

  4. mongodb exception in initAndListen: 12596 old lock file, terminating解决方法

    错误信息如下: exception old lock file, terminating 解决方法 .删除data目录中的.lock文件 .mongod.exe --repair .启动mongod就 ...

  5. BeeProg2C Extremely fast universal USB interfaced programmer

    http://www.elnec.com/products/universal-programmers/beeprog2c/ FPGA based totally reconfigurable 48  ...

  6. solaris 10系统配置工具

    bash-3.2# prtdiag 报告一般系统信息 System Configuration: VMware, Inc. VMware Virtual Platform BIOS Configura ...

  7. iOS开发里的Bundle是个啥玩意?!

    初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...

  8. Remon Spekreijse CSerialPort用法

    在程序中如果要用到多个串口,而且还要做很多复杂的处理,那么最好不用MSComm通讯控件,如果这时你还不愿意自己编写底层,就用这个类:CserialPort类.作者是 Remon Spekreijse ...

  9. nginx做TCP代理实现群集

    nginx做TCP代理实现群集 nginx从版本1.9开始,既能做HTTP代理,又能做TCP代理,这就非常完美了. 配置nginx.conf. 为了简单起见,笔者故意去掉了HTTP代理配置部分,只保留 ...

  10. html 中怎么设置div的位置

    利用CSS的position属性对元素定位,以下是position 属性规定元素的定位类型. absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位.元素的位置通过 ...