开发者

Converting String to float in ios

开发者 https://www.devze.com 2023-04-09 00:07 出处:网络
Hello friends I Have a float t开发者_如何学Pythonype variable ,which contains a total price of specific item,so first I have converted into NSString..below is my code

Hello friends I Have a float t开发者_如何学Pythonype variable ,which contains a total price of specific item,so first I have converted into NSString..below is my code

float total;

[grandTotal setText:[NSString stringWithFormat:**@"$%.2f"**,total]];

so here grandTotal is of type UILabel..where I want the total in the format which I have written in the code

now again when i try to covert to do this

float previousTotal = [[grandTotal text] floatValue];

I am getting value of previousTotal = zero.

So I think their is issue with $ sign how to solve this is issue..


In your case you can solve it by using

float previousTotal = [[[grandTotal text] stringByReplacingOccurrencesOfString:@"$" withString:@""] floatValue];

But I think it is better to pass directly your float value.


I think its better to use

float previousTotal=total;

that is fare and simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号