【问题标题】:NSNumberFormatter IOS Calculator displayNSNumberFormatter IOS计算器显示
【发布时间】:2013-08-02 20:29:04
【问题描述】:

请有人帮帮我。我正在制作计算器应用程序,并且我使用了 NSNumberFormatter 。我想设置在我的 UILabel 上显示的数字限制,例如,最多 12 位数字,但这意味着,任何数字,即使是“1234121.38685 (12)”。因此,如果我将这个数字相乘,它正在增加并且必须正确舍入,并且我希望它是例如“9785123124.12”,如果再次,那么是“75647689751.5”。因此结果值显示整数部分的最大值,而不是浮点数。如何制作?

numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:@" "];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:@"."];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:10];
[numberFormatter setRoundingMode:NSNumberFormatterRoundUp];
[numberFormatter setMaximumIntegerDigits:10];

【问题讨论】:

标签: ios calculator nsnumberformatter


【解决方案1】:

一个是它使用您设置的数字格式化程序将数字转换为 NSString。确保总位数超过 10。然后使用字符串类截断数字。

NSString *numbLong = @"123023.123781237912378123"; // get this from your number formatter
NSString *numbTruncated = [numbLong substringToIndex:10]; // 123023.123

【讨论】:

  • 亚当,这是个好主意,但是如何使这个数字也四舍五入呢?从你的例子我想得到“123023.124”
【解决方案2】:

@亚当·贝林格, 使用此代码获得正确的 RoundMode 会更好!

result=[[self math] calculations]; 
resultString=[NSString stringWithFormat:@"%.16f", result];
 getIntPartOfValue=result; 
stringForGetIntPartOfValue=[NSString stringWithFormat:@"%lld",getIntPartOfValue];
[numberFormatter setMaximumFractionDigits:(maximumAmountOfDigits stringForGetIntPartOfValue.length)+1]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多