Variable interpolation inside printf-style formatting functions

后端 未结 2 619
滥情空心
滥情空心 2020-12-19 06:16

Is there a way to pass a variable for the floating point precision parameter in printf-style string formatting functions in Objective-C (or even C)? For example, in TCL and

2条回答
  •  醉酒成梦
    2020-12-19 06:57

    Yes, the string format specifiers for printf, which are used by Cocoa for formatting, include a variable-precision specifier, * placed after the decimal point:

    int precision = 3;
    NSLog(@"%.*f", precision, 3.14159);
    NSString *myString = [NSString stringWithFormat:@".*f", precision, 3.14159];
    

提交回复
热议问题