如何自动在iphone上插入小数位? [英] How to automatically insert a decimal place on the iphone?

查看:131
本文介绍了如何自动在iphone上插入小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这在过去曾被问过几次,但我尝试的一切都是失败的。我有一个自定义数字键盘与UILabel。当我打1时,UILabel显示一个。现在这里是我想做的:

I know this has been asked a few times in the past but everything I try is failing. I have a custom numeric keypad with a UILabel. When I hit the '1' the UILabel displays a one. Now here's what I'm trying to do:


  • 当我点击'1'按钮时,我想要:0.01在UILabel

  • 后跟一个'4'按钮我想要:0.14在UILabel

  • 然后a'6':1.46

  • etc等。

  • When I hit the '1' button I want: 0.01 in the UILabel
  • Followed by a '4' button I want: 0.14 in the UILabel
  • Then a '6': 1.46
  • etc, etc.

现在键盘和UILabel工作得很好。我只需要得到小数现在工作。任何示例代码将是伟大的,(我应该把它放在我的应用程序)。让我知道,如果你想看到我的代码到目前为止。很简单。感谢大家!

Now the keyboard and UILabel are working great. I just need to get the decimal's working now. Any sample code would be great, (and where I should place it in my application). Let me know if you want to see my code thus far. Pretty straightforward. Thanks everyone!

推荐答案

以下是我如何做的:

#define NUMBER_OF_DECIMAL_PLACES 2
NSMutableArray *typedNumbers = ...; //this should be an ivar
double displayedNumber = 0.0f; //this should also be an ivar

//when the user types a number...
int typedNumber = ...; //the number typed by the user
[typedNumbers addObject:[NSNumber numberWithInt:typedNumber]];
displayedNumber *= 10.0f;
displayedNumber += (typedNumber * 1e-NUMBER_OF_DECIMAL_PLACES);
//show "displayedNumber" in your label with an NSNumberFormatter

//when the user hits the delete button:
int numberToDelete = [[typedNumbers lastObject] intValue];
[typedNumbers removeLastObject];
displayedNumber -= (numberToDelete * 1e-NUMBER_OF_DECIMAL_PLACES);
displayedNumber /= 10.0f;
//show "displayedNumber" in your label with an NSNumberFormatter

警告执行者。使用 NSDecimal 代替 double 的奖励积分。

Typed in a browser. Caveat Implementor. Bonus points for using NSDecimal instead of a double.

解释发生了什么:

我们基本上是做位移位,但是在基数10而不是基数2.当用户键入一个数字(例如:6) ,我们将现有数字移位一个小数位(例如:0.000 => 00.00),将键入的数字乘以0.01(6 => 0.06),并将其添加到我们现有的数字(00.00 => 00.06)。当用户输入另一个数字(例如:1)时,我们做同样的事情。向左移动(00.06 => 00.60),将键入的数字乘以0.01(1 => 0.01),然后添加(00.60 => 00.61)。存储数字的目的是 NSMutableArray 只是一个方便,使删除更容易。但没有必要。只要方便。

We're essentially doing bit shifting, but in base 10 instead of base 2. When the user types a number (ex: 6), we "shift" the existing number left one decimal place (ex: 0.000 => 00.00), multiply the typed number by 0.01 (6 => 0.06), and add that to our existing number (00.00 => 00.06). When the user types in another number (ex: 1), we do the same thing. Shift left (00.06 => 00.60), multiply the typed number by 0.01 (1 => 0.01), and add (00.60 => 00.61). The purpose of storing the number is an NSMutableArray is simply a convenience to make deletion easier. It's not necessary, however. Just a convenience.

当用户点击删除按钮(如果有),我们取最后输入的数字(例如:1),乘以0.01 1 => 0.01),从我们的数字中减去(0.61 => 0.6),然后将我们的数字右移一位小数(0.6 => 0.06)。重复此操作,只要您在输入数字数组中有数字。在数组为空时禁用删除按钮。

When the user hits the delete button (if there is one), we take the last number entered (ex: 1), multiply it be 0.01 (1 => 0.01), subtract it from our number (0.61 => 0.6), and then shift our number right one decimal place (0.6 => 0.06). Repeat this as long as you've got numbers in your array of entered numbers. Disable the delete button when that array is empty.

使用 NSDecimal 的建议是允许非常高精确数字。

The suggestion to use NSDecimal is simply to allow for very high precision numbers.

这篇关于如何自动在iphone上插入小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆