在 NSString 中使用舍入双精度数

发布于 2024-12-19 12:36:46 字数 210 浏览 1 评论 0原文

我遇到的情况是,我有很多不同的 double 值,例如 1.00、0.25 和 2.50。我想对这些双精度数进行四舍五入,使它们成为 1、0.25 和 2.5;换句话说,我想删除所有尾随的 0。有办法做到这一点吗?

目前我一直在使用 %.2f,我想知道是否可以利用它但以某种方式对其进行调整。请问有人可以帮我吗?

I have a situation where I have lots of different double values, for example 1.00, 0.25 and 2.50. I would like to round these doubles so that they become 1, 0.25 and 2.5; in other words I want to remove any trailing 0's. Is there a way to do this?

At the moment I have been using %.2f, and I'm wondering if I can make use of this but adapt it in some way. Please can someone help me out?

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

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

发布评论

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

评论(4

醉南桥 2024-12-26 12:36:46

只要您只讨论显示,这就很简单。您想要的 格式说明符%g :

双参数应以 fe 样式(或以 FE 样式转换> 在 G 转换说明符的情况下),精度指定有效位数 [...] 应从结果的小数部分中删除尾随零[...]

double twopointfive = 2.500;
double onepointzero = 1.0;
double pointtwofive = .25000000000;
NSLog(@"%g %f", twopointfive, twopointfive);
NSLog(@"%g %f", onepointzero, onepointzero);
NSLog(@"%g %f", pointtwofive, pointtwofive);

2011-12-06 21:27:59.180 TrailingZeroes[39506:903] 2.5 2.500000
2011-12-06 21:27:59.184 TrailingZeroes[39506:903] 1 1.000000
2011-12-06 21:27:59.185 TrailingZeroes[39506:903] 0.25 0.250000

相同的格式说明符可以与 NSNumberFormatter 一起使用,这也可以让您对有效数字进行一些控制。

当然,尾随零不能从数字存储在内存中的方式中删除。

As long as you're talking only about display, this is quite easy. The format specifier you want is %g:

The double argument shall be converted in the style f or e (or in the style F or E in the case of a G conversion specifier), with the precision specifying the number of significant digits [...] Trailing zeros shall be removed from the fractional portion of the result[...]

double twopointfive = 2.500;
double onepointzero = 1.0;
double pointtwofive = .25000000000;
NSLog(@"%g %f", twopointfive, twopointfive);
NSLog(@"%g %f", onepointzero, onepointzero);
NSLog(@"%g %f", pointtwofive, pointtwofive);

2011-12-06 21:27:59.180 TrailingZeroes[39506:903] 2.5 2.500000
2011-12-06 21:27:59.184 TrailingZeroes[39506:903] 1 1.000000
2011-12-06 21:27:59.185 TrailingZeroes[39506:903] 0.25 0.250000

The same format specifier can be used with an NSNumberFormatter, which will also give you some control over significant digits.

The trailing zeroes can't be removed from the way the number is stored in memory, of course.

空城缀染半城烟沙 2024-12-26 12:36:46

我相信您希望使用 %g 格式说明符来编辑尾随零。

I believe you want the %g format specifier to redact trailing zeros.

财迷小姐 2024-12-26 12:36:46

并不是真正的四舍五入,但是您是否尝试过 %f 它应该只显示所需的位数,而不是填充数字。

我上面的答案是错误的,正如其他人所说, %g 是正确的方法。

字符串格式化程序的文档也应该有所帮助。 http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265

Not really rounding, but have you tried just %f it should only show the number of digits required rather then padding out the number.

My answer above is wrong, %g as others has stated is the right way to go.

The documentation for string formatters should help too. http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265

多情出卖 2024-12-26 12:36:46

以下是您可以使用的所有格式说明符的列表...

%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

%lld   long long
%llu   unsigned long long
%Lf    long double

Here is a list of all the format specifiers that you can use...

%@     Object
%d, %i signed int
%u     unsigned int
%f     float/double

%x, %X hexadecimal int
%o     octal int
%zu    size_t
%p     pointer
%e     float/double (in scientific notation)
%g     float/double (as %f or %e, depending on value)
%s     C string (bytes)
%S     C string (unichar)
%.*s   Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c     character
%C     unichar

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