- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
例如,计算器最多只能显示 9 个字符。
1/2 -> 0.5
2/3 -> 0.6666667
3/4 -> 0.75
20000/3 -> 6666.6667
123456x123456 -> 1.5241E10
---------
123456789
最佳答案
在 NSNumberFormatter
上设置 setMaximumSignificantDigits:
。
[numberFormatter setUsesSignificantDigits:YES];
[numberFormatter setMaximumSignificantDigits:9];
[numberFormatter setNumberStyle:NSNumberFormatterScientificStyle];
对于超过 9 位的整数,除了使用 NSNumberFormatterScientificStyle
之外,您无能为力。
用一个小程序(见下文)检查后 - 您仍然会遇到多位数指数的问题。您最好拥有多个 NSNumberFormatter
并在显示之前检查数字是多少。当我有更强大的解决方案时,我会快速尝试并更新它。
(直接从 CodeRunner 复制粘贴)
#import <Foundation/Foundation.h>
static void test(NSNumber *n)
{
NSNumberFormatter *f = [NSNumberFormatter new];
[f setUsesSignificantDigits:YES];
[f setMaximumSignificantDigits:9];
[f setNumberStyle:NSNumberFormatterScientificStyle];
NSString *s = [f stringFromNumber:n];
NSLog(@"%@ -> %@ (%lu)", n, s, [s length]);
}
int main(int argc, char *argv[]) {
@autoreleasepool {
test(@(1.0/2));
test(@(2.0/3));
test(@(3.0/4));
test(@(20000.0/2));
test(@(123456*123456));
test(@(0.000000000001));
test(@(123456789.000000000001));
test(@(12345.9999999999999));
}
}
输出
2014-04-24 12:33:01.480 Untitled[3006:507] 0.5 -> 5E-1 (4)
2014-04-24 12:33:01.481 Untitled[3006:507] 0.6666666666666666 -> 6.66666667E-1 (13)
2014-04-24 12:33:01.481 Untitled[3006:507] 0.75 -> 7.5E-1 (6)
2014-04-24 12:33:01.482 Untitled[3006:507] 10000 -> 1E4 (3)
2014-04-24 12:33:01.482 Untitled[3006:507] -1938485248 -> -1.93848525E9 (13)
2014-04-24 12:33:01.483 Untitled[3006:507] 1e-12 -> 1E-12 (5)
2014-04-24 12:33:01.483 Untitled[3006:507] 123456789 -> 1.23456789E8 (12)
2014-04-24 12:33:01.484 Untitled[3006:507] 12346 -> 1.2346E4 (8)
关于ios - 如何使用 NSNumberFormatter 将数字格式化为有限字符的 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23266436/
我有一个关于复杂性的简单问题。我在 Java 中有这段代码: pairs是 HashMap包含 Integer作为键,它的频率为 Collection作为一个值。所以: pairs = new Has
对于我的应用程序,我需要在 Coq 中使用和推理有限映射。谷歌搜索我发现 FMapAVL 似乎非常适合我的需求。问题是文档很少,我还没有弄清楚我应该如何使用它。 作为一个简单的例子,考虑以下使用对列表
我有一个主表tblAssetMaster A和一个移动表tblMovement M。 我想提取所有 Assets 及其当前位置,因此需要获取每个 Assets 的最新移动条目。 字段 A: Asset
我想让我的网站内容居中,但仅限于网页的特定宽度。所以当它超过 500px 时,我希望内容被修复,无法进一步拉伸(stretch)。无论如何都要这样做,还是我最好把所有东西都修好?希望有意义的是添加一些
我正在尝试批量删除 Backbone 模型的集合,如下所示...... collection.each(function(model, i){ model.destroy(); }); 我发现当每
我想要一个软件环境,在其中我可以在具有特定资源的硬件上测试我的软件的速度。例如,当我的主机硬件是具有 12GB RAM 的 3GHz 四核 amd64 时,该程序在具有 24 Mb RAM 的 800
在 Eclipse 中,我得到了 BigInteger.valueOf(2).pow(31093) 的值,但没有得到 BigInteger.valueOf(2).pow(31094) 的值(它是空的)
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
我想将 2 个表从本地 sql server 2000 上传到托管的 mysql。第一个表有 17 列和 680 行,其他 10 列和 8071 行。 我首先使用 xampp mysql 尝试离线,它
我在 S3 中自动生成并保存了静态 html 文件。有时文件大小达到 2mb。是否可以使用javascript来获取html文件的一部分,显示它,当用户到达页面底部时,获取下一部分等等? 最佳答案 X
我是一名优秀的程序员,十分优秀!