Objective-C 如何打印浮点数前导 0?

发布于 2024-09-15 03:43:58 字数 128 浏览 3 评论 0原文

如何使用 NSString 类型打印浮点数的前导零?

Input: 3.14
Desired output: 03.1

Using format @"%02.1f"
Output: 3.1

How do you print out leading zeros for a float using the NSString type?

Input: 3.14
Desired output: 03.1

Using format @"%02.1f"
Output: 3.1

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

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

发布评论

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

评论(2

执手闯天涯 2024-09-22 03:43:58

您需要 @"04.1f"。 4 是宽度。

正如您可以从 文档,格式字符串符合 IEEE printf规范

您指定的格式字符串细分如下:

0 -- 用零填充。
2 -- 整个结果格式化值的最小宽度为 2。
.1 -- 小数点后 1 位的精度。

You want @"04.1f". The 4 is the total width.

As you can see from the documentation, the format strings conform to the IEEE printf specification.

The format string you've specified breaks down as follows:

0 -- Pad with zeros.
2 -- The entire resulting formatted value will have a minimum width of 2.
.1 -- Precision of 1 digit following the decimal point.

寂寞美少年 2024-09-22 03:43:58

由于某种原因,您无法设置浮点数的小数点前宽度(也许这是您应该报告的错误)。这意味着您必须按照您想要的方式拆分十进制格式的数字,然后将它们合并为一个字符串(@"%i.%i", preDec, postDec)。

For some reason you can't set the pre-decimal width on float numbers (maybe it's a bug you should report). This means you will have to split the number at the decimal format each the way you want and then merge them into one string (@"%i.%i", preDec, postDec).

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