如何获取带小数的格式数字(XCode)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14483032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get format numbers with decimals (XCode)
提问by Billy Fleming
My objective is to create a customer calculator application for iPhone and I am using Xcode to write my application. My problem, that I cannot find a solution for, is how to format a number that uses decimals (with extra zeros) without switching into scientific notation
我的目标是为 iPhone 创建一个客户计算器应用程序,我正在使用 Xcode 编写我的应用程序。我的问题,我找不到解决方案,是如何在不切换到科学记数法的情况下格式化使用小数(带有额外零)的数字
I tried...
我试过...
buttonScreen.text = [NSString stringWithFormat:@"%0.f",currentNumber];
%0.f
formatting always rounds so if the user types in "4.23" it displays "4"
%0.f
格式总是四舍五入,所以如果用户输入“4.23”,它会显示“4”
%f
formats numbers with 6 decimals (typing in '5' displays as '5.000000'), but I don't want to show extra zeros on the end of the number.
%f
用 6 位小数格式化数字(输入“5”显示为“5.000000”),但我不想在数字末尾显示额外的零。
%10.4f
is something else that I have seen in my reading to find the solution, but my problem is that I don't know how many decimals will be in the answer, and I may want zero decimals or 10 decimals depending on the number.
%10.4f
是我在阅读中看到的其他解决方案,但我的问题是我不知道答案中有多少个小数,我可能想要零位小数或 10 位小数,具体取决于数字。
The following are examples of numbers I'd like to display (without the commas): A whole number larger than 6 digits, a decimal number with more than 6 digits.
以下是我想要显示的数字示例(不带逗号): 大于 6 位的整数,大于 6 位的十进制数。
123,456,789;
0.123456789;
12345.6789;
-123,456,789;
-0.23456789;
-12345.6789;
*This is a spiritual repost to my earlier question "How to Format Numbers without scientific notation or decimals" which I poorly phrased as I intended to write 'unnecessary (extra zeros),' but upon rereading my post clearly witnessed my inability to convey that at any point in my question.
*这是对我之前的问题“如何在没有科学记数法或小数的情况下格式化数字”的精神上的转贴,因为我打算写“不必要的(额外的零)”,因此措辞不当,但在重新阅读我的帖子时,我清楚地看到我无法传达这一点在我的问题中的任何一点。
回答by Jbryson
Use the NSNumberFormatter class.
First define the formatter:
首先定义格式化程序:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
Then you can define various properties of the formatter:
然后你可以定义格式化程序的各种属性:
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumIntigerDigits = 3;
formatter.minimumFractionDigits = 3;
formatter.maximumFractionDigits = 8;
formatter.usesSignificantDigits = NO;
formatter.usesGroupingSeparator = YES;
formatter.groupingSeparator = @",";
formatter.decimalSeparator = @".";
....
You format the number into a string like this:
您将数字格式化为这样的字符串:
NSString *formattedNumber = [formatter stringFromNumber:num];
Play around with it. Its pretty simple, but may take some work to get the look you would like.
玩弄它。它非常简单,但可能需要一些工作才能获得您想要的外观。
回答by Mike Gledhill
Actually, it makes more sense to use this:
实际上,使用这个更有意义:
label.text = [NSString stringWithFormat:@"%.4f", answer];
This tells XCode to display your number with 4 decimal places, but it doesn't try to "pad" the front of the number with spaces. For example:
这告诉 XCode 用 4 个小数位显示您的数字,但它不会尝试用空格“填充”数字的前面。例如:
1.23 -> " 1.2300" // When using [NSString stringWithFormat:@"%9.4f", answer];
1.23 -> "1.2300" // When using [NSString stringWithFormat:@"%.4f", answer];
回答by Rachel Gallen
try something like this
尝试这样的事情
label.text = [NSString stringWithFormat:@"%9.4f", answer];
where the 9 means total digits (in terms of padding for alignment), and the 4 means 4 decimal places.
其中 9 表示总位数(就对齐填充而言),而 4 表示 4 个小数位。