电话号码的特定数字格式化程序

发布于 2024-12-03 16:35:26 字数 500 浏览 2 评论 0原文

我想以以下格式显示数字

,格式为“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 只有任何人都可以帮助如何做到这一点。

I want to display the the number in the following format

and the format is "123-456-7890"

I am using

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);

I am using the above code to do that but the code is not reflecting ..... and the out put appear as 1234567890 only can any body help how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

错爱 2024-12-10 16:35:26

使用此格式化程序:

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); 

Use this formatter:

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); 
霊感 2024-12-10 16:35:26

在该代码中
检查

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

as

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

PhoneNumberFormatter.m 中的

in that code
check the

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

as

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

in PhoneNumberFormatter.m

淡淡の花香 2024-12-10 16:35:26

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

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

解决方法是指定 ', ' 分隔符并替换它们:

NSNumber *phoneNumber = [NSNumber numberWithDouble:[rxAlertResponse.rxPhoneNo doubleValue]];
NSString *text =[formatter stringFromNumber:phoneNumber];
amountField.text = [text stringByReplacingOccurrencesOfString:@"," withString:@"-"];

setPositiveFormat considers '-' separators to be minus signs.

Note: See Number Format Patterns

A work-around is to specify ',' separators and replace them:

NSNumber *phoneNumber = [NSNumber numberWithDouble:[rxAlertResponse.rxPhoneNo doubleValue]];
NSString *text =[formatter stringFromNumber:phoneNumber];
amountField.text = [text stringByReplacingOccurrencesOfString:@"," withString:@"-"];
向地狱狂奔 2024-12-10 16:35:26

斯威夫特:

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)

Swift :

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文