1

我想以以下格式显示数字

格式为“123-456-7890”

我在用

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setPositiveFormat:@"###-###-####"];
[formatter setLenient:YES];

amountField.text =[formatter stringFromNumber:[NSNumber numberWithDouble:[rxAlertResponse.rxPhoneNo doubleValue]]];
NSLog(@"%@",amountField.text);

我正在使用上面的代码来做到这一点,但代码没有反映.....并且输出显示为 1234567890 只有任何机构才能帮助如何做到这一点。

4

4 回答 4

3

使用此格式化程序:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
    //[formatter setNumberStyle:NSNumberFormatterNoStyle];
    //[formatter setPositiveFormat:@"###-###-####"]; 
    [formatter setGroupingSeparator:@"-"];
    [formatter setGroupingSize:4];
    [formatter setUsesGroupingSeparator:YES];
       [formatter setSecondaryGroupingSize:3];
    //[formatter setLenient:YES];
    NSString *num = @"1234567890";
    NSString *str = [formatter stringFromNumber:[NSNumber numberWithInt:[num intValue]]];
    NSLog(@"%@",str); 
于 2011-09-08T11:37:52.130 回答
1

在该代码中检查

tempStr = [NSString stringWithFormat:@"1(%@)%@-%@", areaCode, phone1, phone2];

作为

tempStr = [NSString stringWithFormat:@"%@-%@-%@", areaCode, phone1, phone2]

在 PhoneNumberFormatter.m

于 2011-09-08T11:41:40.630 回答
0

setPositiveFormat 将“-”分隔符视为减号。

注意:请参阅数字格式模式

一种解决方法是指定“,”分隔符并替换它们:

NSNumber *phoneNumber = [NSNumber numberWithDouble:[rxAlertResponse.rxPhoneNo doubleValue]];
NSString *text =[formatter stringFromNumber:phoneNumber];
amountField.text = [text stringByReplacingOccurrencesOfString:@"," withString:@"-"];
于 2011-09-08T11:36:07.433 回答
0

斯威夫特:

let numberFormatter = NumberFormatter()
numberFormatter.groupingSeparator = "-"
numberFormatter.groupingSize = 4
numberFormatter.usesGroupingSeparator = true
numberFormatter.secondaryGroupingSize = 3
let number = "1234567890"
let formattedNumber = numberFormatter.string(from: NSNumber(value: Int(number)!))
print(formattedNumber as Any)
于 2019-03-11T12:19:49.087 回答