如何以浮点格式编写NSString?

时间:2015-08-12 04:52:18

标签: ios objective-c uitextfield

在我的应用中,我有UITextField。当用户输入123456等文字时,它应该在文本字段中显示为123,456.00

使用以下代码:

UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(300, 120+y*58, 280, 31)];
txt.keyboardType = UIKeyboardTypeDecimalPad;
txt.borderStyle = UITextBorderStyleRoundedRect;
txt.font = [UIFont systemFontOfSize:15];
txt.textAlignment = NSTextAlignmentLeft;
txt.returnKeyType = UIReturnKeyDefault;
txt.delegate = self;
[txt addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

-(void)textFieldDidChange:(UITextField *)theTextField
{
    NSString *textFieldText = [theTextField.text stringByReplacingOccurrencesOfString:@"," withString:@""];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

    NSNumber *number = [formatter numberFromString:textFieldText];
    NSString *formattedOutput = [formatter stringFromNumber:number];

    theTextField.text=formattedOutput;
}

4 个答案:

答案 0 :(得分:1)

使用以下代码

NSString *textFieldText = theTextField.text ;

float f = [theString floatValue];

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];
[formatter setLocale:[NSLocale currentLocale]];

NSLog(@"%@", [formatter stringFromNumber:@(f)]);

将MinimumFractionDigits设置为2& MaximumFractionDigits to 2.在textFieldDidEndEditing textate的委托方法中尝试上面的代码。

答案 1 :(得分:0)

您可以将这样的文字设置为textview。

theTextField.text = [NSString stringWithFormat:@"%.02f", floatVal];

希望这有帮助。

答案 2 :(得分:0)

如果您将这两行添加到您发布的代码段:

[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];

在您将数字格式化程序的样式设置为NSNumberFormatterDecimalStyle的行之后,它应该准确地提供您想要的内容。

@“123456”将导致@“123,456.00” 和@“1”将导致@“1.00”

答案 3 :(得分:0)

你几乎接近你的解决方案,只需要很少的东西需要增强。首先,您必须指定最大和最小允许分数数字,如下所示:

[formatter setMinimumFractionDigits:2];
[formatter setMaximumFractionDigits:2];

它将修复*.00问题,然后还有另一个问题,即将其更新回UI。由于您要设置文本字段文本:

 theTextField.text=formattedOutput;

它将失去当前的编辑位置,因此你将失去理智。如,

您键入了1,上面的代码将使用1.00更新文本字段,光标将移至位置5:

  

1.00

     

^^^^的 ^

如果您将进一步编辑,则无法获得预期结果(文本将尝试在末尾附加)。

要解决此问题,您应该在使用处理程序textFieldDidEndEditing完成编辑时更新文本字段,或者您应该使用其他控件(如UILabel)来显示格式化结果。

示例代码:(用于演示)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(20, 120+(3)*58, 280, 31)];
    txt.keyboardType = UIKeyboardTypeDecimalPad;
    txt.borderStyle = UITextBorderStyleRoundedRect;
    txt.font = [UIFont systemFontOfSize:15];
    txt.textAlignment = NSTextAlignmentLeft;
    txt.returnKeyType = UIReturnKeyDefault;
    txt.delegate = self;
    [self.view addSubview:txt];
    [txt addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

     lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 160+(3)*58, 280, 31)];
    [self.view addSubview:lbl];
}

-(void)textFieldDidChange:(UITextField *)theTextField
{
     NSString *textFieldText = [theTextField.text stringByReplacingOccurrencesOfString:@"," withString:@""];

     NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [formatter setMinimumFractionDigits:2];
    [formatter setMaximumFractionDigits:2];

     NSNumber *number = [formatter numberFromString:textFieldText];
     NSString *formattedOutput = [formatter stringFromNumber:number];

    [lbl setText:formattedOutput];
}