stringwithformat

objective-c stringvalue from double

大憨熊 提交于 2019-12-05 04:47:22
I have the following code: double d1 = 12.123456789012345; NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457 NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235 How do I get a string value that is exactly the same as d1? It may help you to take a look at Apple's guide to String Format Specifiers . %f 64-bit floating-point number %g 64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise Also read up on floating point (in

How does localizedStringWithFormat work?

◇◆丶佛笑我妖孽 提交于 2019-12-04 17:12:11
问题 Please note I am not asking about NSLocalizedString() the macro. I am asking about the NSString class function + (instancetype)localizedStringWithFormat:(NSString *)format, ... . These are two separate things. Question I'm trying to use the NSString class method localizedStringWithFormat but I just can't work out how I'm supposed to use it. Whatever I try I don't seem to get the words to appear in the translation file using Xcode 6 export for localization . I've tried the top two variations

A format specifier such as `%15@` works in NSLog but not with NSString stringWithFormat

别等时光非礼了梦想. 提交于 2019-12-04 10:13:34
I found something strange when trying to use width specifiers with %@ . They work fine in NSLog but not with NSString stringWithFormat: . Example: NSString *rightAligned = @"foo"; NSString *leftAligned = @"1"; NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned); And you get the expected output of: | foo | 1 | But replace the NSLog with stringWithFormat: : NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned]; And the value of test is incorrectly: | foo | 1 | If I change this to use %s and cStringUsingEncoding: then it works: NSString *test2 = [NSString

dynamic string format with NSString stringWithFormat

匆匆过客 提交于 2019-12-03 09:41:09
问题 I've been trying to make a dynamic string format so that through user options precision or padding could be somewhat user defined. An example includes, the padding of leading zeros as in the 24hour time format. In normal time format, the hours can be represented by a single digit, or a digit padded by a zero. This is represented in the string format: ...stringWithFormat:@"Hour: %02i", hour // leading zero padded and ...stringWithFormat:@"Hour: %i", hour // not leading zero padded What I would

Multiple arguments in stringWithFormat: “n$” positional specifiers

不羁岁月 提交于 2019-11-30 09:55:47
问题 In our current implementation we want to change our string arguments (Push notification loc-args) and add new arguments. But we want user's of our old Versions to still use argument #3 and for new user's we want to user argument #4. So in our new implementation we have following code : NSString *format = @"%2$@, %1$@ ,%4$@"; NSArray *arg = @[@"Argument 1", @"Argument 2",@"Argument 3",@"Argument 4"]; NSString *ouptput = [NSString stringWithFormat:format, arg[0], arg[1], arg[2], arg[3]]; OutPut

Multiple arguments in stringWithFormat: “n$” positional specifiers

二次信任 提交于 2019-11-29 17:32:16
In our current implementation we want to change our string arguments (Push notification loc-args) and add new arguments. But we want user's of our old Versions to still use argument #3 and for new user's we want to user argument #4. So in our new implementation we have following code : NSString *format = @"%2$@, %1$@ ,%4$@"; NSArray *arg = @[@"Argument 1", @"Argument 2",@"Argument 3",@"Argument 4"]; NSString *ouptput = [NSString stringWithFormat:format, arg[0], arg[1], arg[2], arg[3]]; OutPut: Argument 2, Argument 1 ,Argument 3 We are expecting it to be Argument 2, Argument 1 ,Argument 4 How

NSString stringWithFormat adding a percent [duplicate]

╄→гoц情女王★ 提交于 2019-11-29 02:49:38
This question already has an answer here: How to add percent sign to NSString 7 answers How can I add a percent to my stringWithFormat function? For example, I'd like to have the following: float someFloat = 40.233f; NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat]; This should cause the string to be: 40.23% But it is not the case. How can I achieve this? % being the character beginning printf-style formats, it simply needs to be doubled: float someFloat = 40.233f; NSString *str = [NSString stringWithFormat:@"%.02f%%",someFloat]; spolu The escape code for a percent sign is “%%”,

NSString stringWithFormat swizzled to allow missing format numbered args

拈花ヽ惹草 提交于 2019-11-28 14:23:41
Based on this SO question asked a few hours ago, I have decided to implement a swizzled method that will allow me to take a formatted NSString as the format arg into stringWithFormat , and have it not break when omitting one of the numbered arg references ( %1$@, %2$@ ) I have it working, but this is the first copy, and seeing as this method is going to be potentially called hundreds of thousands of times per app run, I need to bounce this off of some experts to see if this method has any red flags, major performance hits, or optimizations #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})

NSString stringWithFormat adding a percent [duplicate]

爷,独闯天下 提交于 2019-11-27 17:06:37
问题 This question already has an answer here: How to add percent sign to NSString 7 answers How can I add a percent to my stringWithFormat function? For example, I'd like to have the following: float someFloat = 40.233f; NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat]; This should cause the string to be: 40.23% But it is not the case. How can I achieve this? 回答1: % being the character beginning printf-style formats, it simply needs to be doubled: float someFloat = 40.233f;

NSString stringWithFormat swizzled to allow missing format numbered args

时间秒杀一切 提交于 2019-11-27 08:37:38
问题 Based on this SO question asked a few hours ago, I have decided to implement a swizzled method that will allow me to take a formatted NSString as the format arg into stringWithFormat , and have it not break when omitting one of the numbered arg references ( %1$@, %2$@ ) I have it working, but this is the first copy, and seeing as this method is going to be potentially called hundreds of thousands of times per app run, I need to bounce this off of some experts to see if this method has any red