Say I have a number, double, or NSNumber, 3.3333开发者_如何学Go33
I want that to turn that into @"3.3"
How would I do so?
Can I do NSString stringWithFormat? But what's the format?
NSString *str = [NSString stringWithFormat:@"%.1f", yourDouble];
or for NSNumber:
NSString *str = [NSString stringWithFormat:@"%.1f", [yourNSNumber doubleValue]];
0.0f
means the amount of digits before and after the decimal. So @Wevah's answer would be correct, but keeping that in mind will save you time in the future.
%f stands for a float variable which I am sure you understand. What you would need to display this is a float variable because it contains a decimal. Like people have stated before, you will need to use %.1f The % just tells the compiler that a special character is coming up. The f tells the compiler that it is a float variable. The .1 tells the compiler how many decimal places your float variable is to have. If you would want to have 6 decimal places, then you would us %.6f
Yes, you will want to use string with format. Say you have a UILabel, then you will want to say
theLabel'sName.text = [NSString stringWithFormat:@"%.1f", ((float)int1 / int2)];
You need the (float) to tell the compiler that whatever int1 / int2 is, is a float variable.
If your NSNumber
instance is called myNumber
then do
[myNumber stringValue];
精彩评论