Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-C - other methods to convert a float to string

I know that you can convert a float into a string using

NSString *myString = [NSString stringWithFormat:@"%f", myFloat]; 

My question is what OTHER methods exist in Objective-C that can do that same?

thanks.

like image 515
Duck Avatar asked Feb 14 '11 14:02

Duck


People also ask

Can we convert float to string in C?

gcvt() | Convert float value to string in C This function is used to convert a floating point number to string. Syntax : gcvt (float value, int ndigits, char * buf); float value : It is the float or double value. int ndigits : It is number of digits.

Can we convert float to string?

The Float. toString() method can also be used to convert the float value to a String. The toString() is the static method of the Float class.

Which function is used to convert string to float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number.

How do I convert a float to a string in C ++?

We can convert float and double to string using the C++11 std::to_string() function. For the older C++ compilers, we can use std::stringstream objects.


2 Answers

You could use NSNumber

NSString *myString = [[NSNumber numberWithFloat:myFloat] stringValue]; 

But there's no problem doing it the way you are, in fact the way you have in your question is better.

like image 68
Jonathan. Avatar answered Sep 28 '22 01:09

Jonathan.


in a simple way

NSString *floatString = @(myFloat).stringValue; 
like image 24
ram880 Avatar answered Sep 28 '22 01:09

ram880