Objective c 如何使用参数中的位数在obj-c中打印格式化的浮点

Objective c 如何使用参数中的位数在obj-c中打印格式化的浮点,objective-c,Objective C,我知道用特定数字设置格式化浮点的方法 NSLog(@"%.2f", myFloat); 设置参数编号的方法是什么?像这样的 cell.lblOpenPrice.text = [NSString stringWithFormat:@"%.%if", trade.open_price, trade.digits]; 您应该为此使用的实例。有几十种选择,太多了,无法在这里讨论。即。如前所述,您可以设置总位数(有效位数)或整数位数和小数位数 要动态设置字段精度,请在格式中使用星号,并首先提供精度参数

我知道用特定数字设置格式化浮点的方法

NSLog(@"%.2f", myFloat);
设置参数编号的方法是什么?像这样的

cell.lblOpenPrice.text = [NSString stringWithFormat:@"%.%if", trade.open_price, trade.digits];

您应该为此使用的实例。有几十种选择,太多了,无法在这里讨论。即。如前所述,您可以设置总位数(有效位数)或整数位数和小数位数


要动态设置字段精度,请在格式中使用星号,并首先提供精度参数,因此您的代码示例如下:

cell.lblOpenPrice.text = [NSString stringWithFormat:@"%.*f", trade.digits, trade.open_price];

HTH

谢谢!NSNumberFormatter*格式化程序=[NSNumberFormatter new];[formatter setMinimumFractionDigits:trade.digits];[格式化程序设置MaximumFractionDigits:trade.digits];[格式化程序设置最小整数位数:1];
cell.lblOpenPrice.text = [NSString stringWithFormat:@"%.*f", trade.digits, trade.open_price];