Reputation: 39081
I'm wondering about something I saw with format specifiers. What I saw was this:
@"%03.1f", someFloat
I tested it and it returned in the log
"1.5"
What is this called, the thing with 03.1
between %f
?
Upvotes: 2
Views: 9742
Reputation: 195
%w.pf
where
w -> Minimum width of total value
p -> Exact number of decimal after .
float f = 2345.34567;
NSLog(@"%2.2f",f); //Prints: "2345.35"
NSLog(@"%.4f",f); //Prints: "2345.3556"
NSLog(@"%8.2f",f); //Prints: " 2345.35"(includes 1 space to make width of f '8')
NSLog(@"%15.2f",f); //Prints: " 2345.35"(includes 8 spaces to make width of f '15')
Upvotes: 5
Reputation: 107121
This is a basic question. It is from C language.
A default floating value can be formatted like:
%w.pf
Here:
w stands for width
p stands for precision
Please check C format specifiers
Example:
Printing 3.141592 using %f displays 3.141592 Printing 3.141592 using %1.1f displays 3.1 Printing 3.141592 using %1.2f displays 3.14 Printing 3.141592 using %3.3f displays 3.142 Printing 3.141592 using %4.4f displays 3.1416 Printing 3.141592 using %4.5f displays 3.14159 Printing 3.141592 using %09.3f displays 00003.142 Printing 3.141592 using %-09.3f displays 3.142 Printing 3.141592 using %9.3f displays 3.142 Printing 3.141592 using %-9.3f displays 3.142
Upvotes: 7
Reputation: 29064
A width (%3f
) says that we want three digits (positions) reserved for the output.
%3.1f
-> (print as a floating point at least 3 wide and a precision of 1)
Read Format Specifiers
Upvotes: 1