Displaying a float to a number of decimals
Seem开发者_StackOverflow中文版s an easy question. And it can be done using the following format "%.#f". Replacing # with a number will give you that number of decimals. But what if you want to pass in an NSInteger variable to specify the value of #?
I am trying to display a label containing a price with the number of decimals the user had previously specified. Is this possible?
The following code is what a want to change. I want to substitute the hardcoded number (in this case "4") with a variable of type NSInteger.
lblPrice.text = [NSString stringWithFormat:@"%.4f",val_stdPrice];
Any help in this matter would be greatly appreciated.
Construct the formatting string dynamically:
NSString *fmtString = [NSString stringWithFormat:@"%%.%df", 17];
NSString *resultString = [NSString stringWithFormat:fmtString, 1.337f];
NSLog (resultString);
Retrieve the Integer and append it to the string to match the format. Then use the string as a parameter.
精彩评论