[IOS]NSString转int数据丢失
从textview上面获取nsstring再转int会数据丢失,这是因为string里面可能有特殊字符,例如空格,逗号
在我的代码里,1,140转后变成1,所以使用NSNumber:
NSString * balance = [self.balanceLb.text componentsSeparatedByString:@"HKD"][1]; NSLog(@"balance = %@",balance); NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; f.numberStyle = NSNumberFormatterDecimalStyle; NSNumber *myNumber = [f numberFromString:balance]; float balanceFloat = [myNumber floatValue]; NSLog(@"balanceFloat = %f",balanceFloat); //Wrong, miss data below float balanceFloat = [balance floatValue]; NSLog(@"balanceInt = %f",balanceFloat);
相关推荐
zhujiangtaotaise 2020-01-01
84407518 2012-07-16
AllenG 2013-07-07
Beatopcoder 2013-07-14
afrtooylsw 2014-05-14
让编程改变世界 2015-03-13
shqhope 2015-04-09
zoutian00 2015-05-12
87214552 2015-10-27
84407518 2016-01-25
89283517 2012-06-19
ObjectiveC 2012-06-14
86981633 2019-06-29
我的iOS王者之路 2019-06-28
好好学习天天 2019-06-28
jscjxysx 2019-06-28