我有以下代码:
double d1 = 12.123456789012345;
NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457
NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235
Run Code Online (Sandbox Code Playgroud)
如何获得与d1完全相同的字符串值?
我希望能够使用空格右对齐字符串.我必须能够使用该stringWithFormat:方法.
到目前为止,我已经尝试了推荐的格式,它似乎不起作用:[NSString stringWithFormat:@"%10@",@"test"].
我希望这会返回一个有六个空格后跟"test"的字符串,但我得到的只是"test",没有空格.
我发现%g只在需要时显示小数.如果数字是整数,则不添加尾随.000,这样就好了.但是在例如1.12345的情况下,我希望它将答案缩短为1.123.在1.000的情况下,我想只显示1,因为%g已经存在.
我试图在字符串中指定%.3g,但这不起作用.如果有人有答案,我将不胜感激!
我试图用分段控件设置浮点数的位数.
所以我用"0","1","2"和"3"创建了一个分段控件.我想用逗号(self._segmentedControl.selectedSegmentIndex)设置逗号后面的数字.
我知道我可以决定逗号之后应该有多少位数:
sliderValue.text = [NSString stringWithFormat:@"%.3f",slider.value];
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
为什么方法[NSString stringWithFormat]返回一个id类型?从我期望的名称,它返回一个NSString,而不是一个通用的指针.其他课程遵循这个"规则".例如,[NSNumber numberWithInt]返回a NSNumber,而不是a id.
我认为从工厂方法这样的事实来说甚至都不合理.
我在使用NSString stringWithFormat做某事时遇到了问题......
问题如下:
首先,我有2个字符串.哪些是货币数量.
问题是,我想用这两个创建1个字符串.
例如:
strF = [NSString stringWithFormat:@"First: %@ Second: %@", value1, value2];
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能在第一个和第二个之间做出正确的填充.假设第一个字符串长度不一样...
我想这样做:
First: 10,000.00 Second: 788.00
First: 10.00 Second: 788.00
First: 0.00 Second: 788.00
First: 100.00 Second: 788.00
First: 5.00 Second: 788.00
Run Code Online (Sandbox Code Playgroud) 尝试将宽度说明符与%@. 他们的工作罚款NSLog,但不与NSString stringWithFormat:。
例子:
NSString *rightAligned = @"foo";
NSString *leftAligned = @"1";
NSLog(@"| %15@ | %-15@ |", rightAligned, leftAligned);
Run Code Online (Sandbox Code Playgroud)
你会得到预期的输出:
| foo | 1 |
Run Code Online (Sandbox Code Playgroud)
但更换NSLog同stringWithFormat::
NSString *test = [NSString stringWithFormat:@"| %15@ | %-15@ |", rightAligned, leftAligned];
Run Code Online (Sandbox Code Playgroud)
并且 的值test不正确:
| foo | 1 |
Run Code Online (Sandbox Code Playgroud)
如果我将其更改为使用%s,cStringUsingEncoding:然后它可以工作:
NSString *test2 = [NSString stringWithFormat:@"| %15s | %-15s |", [rightAligned cStringUsingEncoding:NSUTF8StringEncoding], [leftAligned cStringUsingEncoding:NSUTF8StringEncoding]];
Run Code Online (Sandbox Code Playgroud)
结果与 相同NSLog。
真正奇怪的是它NSLog …
我有一个输入,UIKeyboardTypeDecimalPad我需要我的用户输入一个浮点(点后面的无限字符).输入后我用以下内容过滤字符串:
NSString *newValue = [NSString stringWithFormat:@"%.f",[textField.text floatValue]]
Run Code Online (Sandbox Code Playgroud)
但是这给了我一个点后的许多不必要的数字(例如2.25它给出了2.249999).
我只需要过滤输入,这将是一个合法的浮点数(数字和不超过一个点).
我怎么做?
使用predicateWithFormat,%@被""包围.我们需要使用%K作为密钥.
例如[NSPredicate predicateWithFormat @"%@ == %@" ,@"someKey",@"someValue"]变成
"someKey" == "someValue"
Run Code Online (Sandbox Code Playgroud)
在stringWithFormat时,%@不被""包围
[NSString stringWithFormat @"%@ == %@" ,@"someKey",@"someValue"]
Run Code Online (Sandbox Code Playgroud)
变 someKey == someValue
为什么不同?
我错过了什么吗?
为什么在predicateWithFormat中使用%@作为"Value",因为它不是string @WithFormat中%@的含义.为什么不创建一个新的表示法,比如说%V来获得"Value"而%@保持值就像在stringWithFormat中一样.
为什么Apple决定使用相同的符号,即%@应该有不同的含义.
他们真的不一样吧?我错过了什么吗?
这似乎是一个超级基本的问题,但我似乎无法在任何地方找到答案:-(我能够在Objective C中做到这一点,但我陷入了Swift的困境.
我需要做什么:
stringWithFormat等效方法将值注入另一个字符串(因为另一个字符串也是本地化的,下面的简化示例中未显示)如何在Objective C中轻松完成- 这有效:
// points is of type NSNumber *
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.locale = [NSLocale currentLocale];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *ptsString = [formatter stringFromNumber:points];
NSString *message = [NSString stringWithFormat:@"You've earned %@ points", ptsString];
Run Code Online (Sandbox Code Playgroud)
我最好尝试在Swift中执行此操作- 最后一行编译错误:
// points is of type Int
let formatter = NSNumberFormatter()
formatter.locale = NSLocale.currentLocale()
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
let ptsString = formatter.stringFromNumber(points)!
let message = String(format: "You've earned %@ points", arguments: ptsString) …Run Code Online (Sandbox Code Playgroud) stringwithformat ×10
ios ×7
nsstring ×5
objective-c ×5
iphone ×3
double ×1
nslog ×1
predicate ×1
string ×1
swift ×1