NSString - 浮点数最多 1 位小数

发布于 2024-08-30 08:18:20 字数 142 浏览 5 评论 0原文

我想在 NSString 中使用浮点数。我使用 stringWithFormat 和 %f 将浮点数集成到 NSString 中。问题是我只想显示一位小数 (%.1f) 但当没有小数时我不想显示 '.0' 。

我怎样才能做到这一点?

谢谢

I would like to use a float in a NSString. I used the stringWithFormat and a %f to integrate my float into the NSString. The problem is that I would like to display only one decimal (%.1f) but when there is no decimals I don't want to display a '.0' .

How can I do that?

Thanks

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

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

发布评论

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

评论(4

南冥有猫 2024-09-06 08:18:20

我用 NSNumberFormatter 和 setMaximumFractionDigits 找到了答案,然后:

[numberFormatter stringFromNumber:myNumber]

感谢大家,尤其是@falconcreek

I found the answer with NSNumberFormatter and setMaximumFractionDigits, then:

[numberFormatter stringFromNumber:myNumber]

Thanks to everyone especially @falconcreek

国产ˉ祖宗 2024-09-06 08:18:20

您应该使用NSNumberFormatter

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormat:@"#,##0.#"];
NSNumber *oneThousand = [NSNumber numberWithFloat:1000.0];
NSNumber *fivePointSevenFive = [NSNumber numberWithFloat:5.75];

NSLog(@"1000.0 formatted: %@", [numberFormatter stringFromNumber:oneThousand]);
NSLog(@"5.75 formatted: %@", [numberForatter stringFromNumber:fivePointSevenFive]);

Apple 的数据格式化编程指南中有一个指向格式化标准的链接。
方便参考数字格式模式

You should use NSNumberFormatter.

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormat:@"#,##0.#"];
NSNumber *oneThousand = [NSNumber numberWithFloat:1000.0];
NSNumber *fivePointSevenFive = [NSNumber numberWithFloat:5.75];

NSLog(@"1000.0 formatted: %@", [numberFormatter stringFromNumber:oneThousand]);
NSLog(@"5.75 formatted: %@", [numberForatter stringFromNumber:fivePointSevenFive]);

There is a link in Apple's Data Formatting Programming Guide to the formatting standards.
Handy Reference Number Format Patterns

一场春暖 2024-09-06 08:18:20

你可以像这样使用 %g

NSLog([NSString stringWithFormat: @"test: %g", (float)1.2]);
NSLog([NSString stringWithFormat: @"test: %g", (float)1])

you could use %g like this

NSLog([NSString stringWithFormat: @"test: %g", (float)1.2]);
NSLog([NSString stringWithFormat: @"test: %g", (float)1])

NSNumberFormatter 最近发生了一些变化。以下是对两位有效数字进行四舍五入的示例:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
[numberFormatter setUsesSignificantDigits:YES];
[numberFormatter setMaximumSignificantDigits:2];

这里有链接,指向一个好的最新资源。

NSNumberFormatter has changed a little bit recently. Here is an example of getting two significant digits rounding:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterNoStyle];
[numberFormatter setUsesSignificantDigits:YES];
[numberFormatter setMaximumSignificantDigits:2];

And here's a link to a good, recent resource.

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