简体   繁体   中英

NSString to char (not const char)

How can I convert NSString to char ? Note - char not const char . I have float number, I need to convert it so that only 2 values after . would be left. So I use NSString to do that. After that I need to make NSNumber from that. So I decided to convert NSString to char and after that to NSNumber. So I tried

char* someCharINeed = [SomeString UTF8String];

And it says

Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'

If it is used like that

const char* someCharINeed = [SomeString UTF8String];

it works but NSNumber

[NSNumber numberWithChar:someCharINeed];

says

Cannot initialize a parameter of type 'char' with an lvalue of type 'const char *'

而不是转换为字符串,另一种方法是将数字乘以100,将答案转换为整数,然后除以100-这将为您提供小数点后2位。

float newValue = floorf(oldValue*100)/100;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM