如何格式化数字以使其始终有一个小数位?

发布于 2024-09-25 00:13:46 字数 682 浏览 0 评论 0原文

例如,我的浮点值如下:

0.2 1.5 2.0 3.0 10.0 52.5 60.0

无论如何,我真的想要一位小数。即使数字正好是 1,我希望它显示为 1.0

我尝试像这样设置 NSNumberFormatter,但它不起作用:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setAllowsFloats:YES];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setMinimumFractionDigits:1];

[formatter setUsesSignificantDigits:YES];
[formatter setMinimumSignificantDigits:2];

[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];

它显示 0.2、5.0、5.5、9.0,

但是当它达到 10 时,它将显示 10, 10.3, 11, 11.9, 15, ... 等等。 这是 NSNumberFormatter 无法做到的吗?

For example, I have float values like:

0.2
1.5
2.0
3.0
10.0
52.5
60.0

Under any circumstances, I really want one fractional digit. Even if the number is exactly 1, i want it to be displayed as 1.0

I tried to set up an NSNumberFormatter like this, but it doesn't work:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setAllowsFloats:YES];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setMinimumFractionDigits:1];

[formatter setUsesSignificantDigits:YES];
[formatter setMinimumSignificantDigits:2];

[formatter setNumberStyle:kCFNumberFormatterDecimalStyle];

It shows 0.2, 5.0, 5.5, 9.0,

but when it reaches 10, it would show 10, 10.3, 11, 11.9, 15, ... and so on.
Is this something NSNumberFormatter can't do?

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

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

发布评论

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

评论(3

怪我鬧 2024-10-02 00:13:46

setAlwaysShowsDecimalSeparator:
控制接收器是否始终显示小数分隔符,即使对于整数也是如此。

尝试将其设置为 YES

setAlwaysShowsDecimalSeparator:
Controls whether the receiver always shows the decimal separator, even for integer numbers.

Try setting this to YES

凡间太子 2024-10-02 00:13:46

试试这个?

NSString * f= [NSString stringWithFormat:@"%.01f",(float)10];

try this?

NSString * f= [NSString stringWithFormat:@"%.01f",(float)10];

﹏半生如梦愿梦如真 2024-10-02 00:13:46

我无法使用 madmi3 解决方案,因为我需要动态设置 NSNumberFormatter 的区域设置。

所以最终使用了以下解决方案:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.alwaysShowsDecimalSeparator = YES; // This will produce results like 10. for intergers
formatter.maximumFractionDigits = 1;
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"pt"]; // I get this locale dinamically

NSString *formattedNumber = [formatter stringFromNumber:[NSNumber numberWithInteger:10]];

//Check this formattedNumber is ending with the separator
//If so, then append one more 0.
if ([formattedNumber hasSuffix:[formatter.locale objectForKey:NSLocaleDecimalSeparator]])
{
    formattedNumber = [formattedNumber stringByAppendingString:@"0"];
}

NSLog(@"%@", formattedNumber);

I could not use madmi3 solution's because I need to set the locale of the NSNumberFormatter dynamically.

So ended up using the following solution:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.alwaysShowsDecimalSeparator = YES; // This will produce results like 10. for intergers
formatter.maximumFractionDigits = 1;
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"pt"]; // I get this locale dinamically

NSString *formattedNumber = [formatter stringFromNumber:[NSNumber numberWithInteger:10]];

//Check this formattedNumber is ending with the separator
//If so, then append one more 0.
if ([formattedNumber hasSuffix:[formatter.locale objectForKey:NSLocaleDecimalSeparator]])
{
    formattedNumber = [formattedNumber stringByAppendingString:@"0"];
}

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