On an iPhone keypad, where’s the decimal point?

While recently developing an iPhone app i needed a numeric keypad to appear to input a number. The only problem is that the built-in numeric keypad doesn’t contain a decimal point, making entering floating point numbers impossible.

After reading the documentation there is however a few solutions.

  1. Add a user created button to the standard numeric keypad.
  2. Use the new  UIKeyboardTypeDecimalPad  keypad type which was implemented in iOS v4.1

I opted for number 2, both because it’s easier to implement and because the decimal point functionality wasn’t a critical feature for users of lesser iOS versions. The problem of the user not having iOS4.1 installed can be tested for before implementing the updated decimal keypad.

First of all the new  UIKeyboardTypeDecimalPad  type is not available in Interface Builder v3.2.5 (the latest version as of writing) making assigning this new type a little more difficult than selecting it from the drop-down menu. The screenshot above shows the only numeric keypad type available. I’m sure the next version of Interface Builder will have this new type available, but we can’t wait until then. So lets get working with what we’ve got.

First, inside Interface Builder, drag a Text Field onto the View and bring up the Attribute Panel. In there, under the ‘Text Input Traits’ category select ‘Number Pad’ from the ‘Keyboard’ drop-down menu. This is the fallback keypad type in case the user is not using iOS 4.1+. Once that’s done we need to modify the code in this view’s controller.

In the view controller look for the  - (void)viewDidLoad  method. This method is called when the view loads successfully and so it’s where you should do any sort of initialisation.

Inside that method add this code:

Here, we test that the  UIKeyboardTypeDecimalPad  type is available that the  systemVersion  is above or equal to 4.1. If these are both true we can assign the  UIKeyboardTypeDecimalPad  type to our Text Field. Here is the result:

The left image shows the standard numeric keypad and the other image shows the decimal keypad if the user is running iOS 4.1 or greater.

Submit a comment

1 Comment

  1. I need to have the decimal point on my iPhone so I can add and subtract numbers

Submit a Comment

We would love to hear your thoughts. Feel free to submit a comment and join the conversation!

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">