简体   繁体   English

NSNumberFormatter的问题

[英]Problems with NSNumberFormatter

Im working on a calculator app which works with doubles and then when you press equals the operation is done with doubles then switched to NSNumberFormatter. 我正在使用计算器应用程序,它使用双打,然后当你按下等于操作完成双打然后切换到NSNumberFormatter。 My problem is when the user wants to add (or -,/,*) a number to the answer on the screen. 我的问题是当用户想要在屏幕上的答案中添加(或 - ,/,*)数字时。 If the number on the screen has no commas, everything works fine, if the number on the screen does have commas I get a random number instead of the correct answer. 如果屏幕上的数字没有逗号,一切正常,如果屏幕上的数字确实有逗号,我会得到一个随机数而不是正确的答案。 Any suggestions? 有什么建议么?

here is my code for the equals button. 这是我的等于按钮的代码。 I have the same code for every operation. 我对每个操作都有相同的代码。 Please excuse my terrible naming 请原谅我糟糕的命名

also test.firstnumbers is aa string that stores the numbers before the user enters in the second set of numbers for the math problem test.firstnumbers也是一个字符串,用于在用户输入数学问题的第二组数字之前存储数字

    double number = [test.firstNumbers doubleValue];

    double otherNumber = [self.label.text doubleValue];

    double answer = number + otherNumber;

    NSNumberFormatter *finalAnswer = [[NSNumberFormatter alloc]init];

    [finalAnswer setNumberStyle:NSNumberFormatterDecimalStyle];

    self.label.text = [finalAnswer stringFromNumber:[NSNumber numberWithDouble:answer]];

double otherNumber = [self.labelTwo.text doubleValue]; double otherNumber = [self.labelTwo.text doubleValue];

You never want to use the view that displays the results as the storage for your data. 您永远不想使用显示结果的视图作为数据存储。 That's a violation of the MVC paradigm, and just generally a poor plan. 这违反了MVC范式,而且通常是一个糟糕的计划。 You should have the number in the display stored in your data model (even if that "model" is as simple as an instance variable in your view controller). 您应该将显示中的数字存储在数据模型中(即使该“模型”与视图控制器中的实例变量一样简单)。 Among other benefits, this avoids the need to convert the number to text and then back to a number, and therefore entirely avoids the problem you're having with commas. 除了其他好处之外,这还避免了将数字转换为文本然后再转换为数字的需要,因此完全避免了您使用逗号时出现的问题。

Taking a look at the direct cause of the problem, you're using NSString's -doubleValue method to convert from string to number, but using the number formatter to convert from number to string. 看一下问题的直接原因,您使用NSString的-doubleValue方法将字符串转换为数字,但使用数字格式器将数字转换为字符串。 As you've seen, the two aren't exactly complementary. 如你所见,这两者并不是完全互补的。 Use the number formatter for both conversions and you'll have better luck. 使用数字格式化器来进行两次转换,你会有更好的运气。 But, again, you really shouldn't be using the view to hold the operand. 但是,再次,你真的不应该使用视图来保存操作数。

It's not random, it's entirely predictable but wrong, because doubleValue doesn't interpret commas. 它不是随机的,它完全可以预测但是错误,因为doubleValue不解释逗号。

When the current string displayed is 22,222,223 and you try to add 2 , you try to get the double value of 22,222,223 by using doubleValue . 当显示的当前字符串是22,222,223并且您尝试添加2 ,您尝试使用doubleValue获取22,222,223的double值。 But doubleValue doesn't understand commas, so it stops when it reaches the first one. 但是doubleValue不理解逗号,因此它在到达第一个时停止。 So instead of 22222223 , you get a value of 22 . 因此,不是22222223 ,而是获得22的值。 The code adds 2 to it and gets 24 , which is what you see. 代码为它添加2并获得24 ,这就是你所看到的。

To read and parse a number that contains commas, you would use NSNumberFormatter again, this time with the numberForString method. 要读取和解析包含逗号的数字,可以再次使用NSNumberFormatter ,这次使用numberForString方法。 But, as @Caleb points out, that's a really bad design. 但是,正如@Caleb指出的那样,这是一个非常糟糕的设计。 You should store the value in an attribute somewhere in code and use that when performing the math. 您应该将值存储在代码中的某个属性中,并在执行数学时使用它。 Don't read data from your UI unless it's something the user just entered. 除非用户刚刚输入,否则不要从UI读取数据。 Never write data to the UI and then read it back unless there's some way for the user to change that data. 永远不要将数据写入UI然后将其读回,除非用户有某种方式来更改该数据。

I agree with Caleb, you can separate better the model (numbers computation) to the rest of views+controllers. 我同意Caleb,你可以将模型(数字计算)更好地分离到其他视图+控制器。

However I think that the problem is into 'double value' conversion from string: replace all commas with dots can be the trick. 但是我认为问题在于从字符串转换为'double value':用点替换所有逗号都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM