如果需要,正确使用格式说明符以显示最多三位小数,否则显示零位小数?

发布于 2024-12-02 15:00:53 字数 171 浏览 3 评论 0原文

我发现 %g 在需要时仅显示小数。如果数字是整数,则不会添加尾随 0.000,这样就很好。 但在例如 1.12345 的情况下,我希望它将答案缩短为 1.123。 在 1.000 的情况下,我只想显示 1,就像 %g 已经做的那样。

我尝试在字符串中指定 %.3g,但这不起作用。 如果有人有答案,我将不胜感激!

I've found %g to show only decimals if needed. If the number is whole, no trailing .000 is added, so thats good.
But in the case of for example 1.12345 I want it to short the answer to 1.123.
And in the case of 1.000 I want to only show 1, as %g already does.

I've tried to specify %.3g in the string, but that doesn't work.
If anyone has the answer, I'd be grateful!

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

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

发布评论

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

评论(3

情栀口红 2024-12-09 15:00:53

我通过 IEEE 规范 回顾了“格式字符串”的功能据我了解,您希望的行为是不可能的。

我建议您使用 NSNumberFormatter 类。我写了一个符合您希望的行为的示例。我希望这有帮助:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setDecimalSeparator:@"."];
[numberFormatter setGroupingSeparator:@""];
NSString *example1 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:123456.1234]];
NSLog(@"%@", example1);
NSString *example2 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:123456.00]];
NSLog(@"%@", example2);

I reviewed the abilities of a "format string" via the IEEE Specification and as I understand it your wished behavior is not possible.

I recommend to you, to use the NSNumberFormatter class. I wrote an example that matches your wished behavior. I hope that helps:

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setDecimalSeparator:@"."];
[numberFormatter setGroupingSeparator:@""];
NSString *example1 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:123456.1234]];
NSLog(@"%@", example1);
NSString *example2 = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:123456.00]];
NSLog(@"%@", example2);
书信已泛黄 2024-12-09 15:00:53

NSLog(@"%.3g", 1.12345) 会得到什么?

我做了一些测试,据我了解你的问题,你走在正确的轨道上。这些是我的结果:

NSLog(@"%g", 1.000000);    => 1
NSLog(@"%g", 1.123456789);  => 1.12346
NSLog(@"%.1g", 1.123456789);  => 1
NSLog(@"%.2g", 1.123456789);  => 1.1
NSLog(@"%.3g", 1.123456789);  => 1.12
NSLog(@"%.4g", 1.123456789);  => 1.123

要得到你想要的,请使用@“%.4g”。

What do you get for NSLog(@"%.3g", 1.12345)?

I did some tests and as I understand your question you're on the right track. These are my results:

NSLog(@"%g", 1.000000);    => 1
NSLog(@"%g", 1.123456789);  => 1.12346
NSLog(@"%.1g", 1.123456789);  => 1
NSLog(@"%.2g", 1.123456789);  => 1.1
NSLog(@"%.3g", 1.123456789);  => 1.12
NSLog(@"%.4g", 1.123456789);  => 1.123

To get what you want use @"%.4g".

你与清晨阳光 2024-12-09 15:00:53

以下是 Jan 针对 Swift 4 的解决方案:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 2
numberFormatter.decimalSeparator = "."
numberFormatter.groupingSeparator = ""
let example1 = numberFormatter.string(from: 123456.1234)!
print(example1)
let example2 = numberFormatter.string(from: 123456.00)!
print(example2)

Here is Jan's solution for Swift 4:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 2
numberFormatter.decimalSeparator = "."
numberFormatter.groupingSeparator = ""
let example1 = numberFormatter.string(from: 123456.1234)!
print(example1)
let example2 = numberFormatter.string(from: 123456.00)!
print(example2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文