hockeyman
hockeyman

Reputation: 1183

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 *'

Upvotes: 1

Views: 1788

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Rather than converting to a string, another way to do this is to multiple the number by 100, convert the answer to an int and then divide by 100 -- that will give you 2 places after the decimal point.

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

Upvotes: 1

Related Questions