Number Padding to Strings

Hi,
I'm wanting to take a number and left pad it with 0's into a string.
I'm currently using
NSString foo = [NSString stringWithFormat: @"%d", bar1];
to convert an int to a string. But i'm wanting to make sure this string is the same length by left padding with 0's. How do i achieve this?
eg....
24 = @"000024"
Thanks.
N

jaxjason wrote:
This is what Charles helped me write on the other post. turns out there is a premade NSString function for creating a string with padding. * stringByPaddingToLength:length* above.
Actually, I think there is an even better way to do it. Look up the NSNumberFormatter. Here is a slightly modified example that pads with '-' characters, for example:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4];
[numberFormatter setFormatWidth: 10];
[numberFormatter setPadCharacter: @"-"];
NSNumber *four = [NSNumber numberWithFloat:4.0];
NSLog(@"%@", [numberFormatter stringFromNumber:four]);
// output: "---------4"
I haven't tried the above code so I don't know if it will work. But if there is a formal, safer Cocoa replacement for printf, this is it.
I was used to things like VB where there really isn't a "label:Variable" structure.
Objective-C is actually a pretty old language. This is a holdover from the older, more "pure" object-oriented languages. The only thing the "label" is used for is to differentiate method signatures. Without the label, if two methods have the same name and same argument types, they are considered the same method. Objective-C expands the method name to include all the labels. This allows more freedom in defining the first part of a method and and argument types.
Thanks alot for you help on this. I am really excited i figured this out. Although my wife may not like it so much, I now have even more reason to work on this stuff.
Consider getting a laptop. Then you can sit next to her and code away. It helps.

Similar Messages

  • Format a number, pad with spaces

    I want a simple way to format a number into a String, but have it padded with spaces. My code started like this:
    DecimalFormat myFormatter = new DecimalFormat("###.00");
    String output = myFormatter.format(12.5);
    The output is '12.50' when I want ' 12.50' (leading space). There's got to be an easy way to do this!
    Thanks for your help

    What you probably wanted to do was print some numbers in a column (of JTextArea's, for example) so that the decimal points line up.
    I wasted some more time and came up with the following:  String formattedNumber = numberPad(number, "###.00E0");
      static String numberPad(double number, String format) {
        int desiredDecimalPosition = format.indexOf('.');
        DecimalFormat numFormat = new DecimalFormat(format);
        String formattedNum = numFormat.format(Math.abs(number));
        formattedNum = formattedNum.replace('E', 'e');
        int exponentPosition = formattedNum.indexOf('e');
        if (exponentPosition < 0)
          exponentPosition = formattedNum.length();
        formattedNum = formattedNum.replaceFirst("e0", "");
        int decimalPosition = formattedNum.indexOf('.');
        formattedNum = (number > 0 ? " " : "-") + formattedNum;;
        if (decimalPosition < 0)
          decimalPosition = exponentPosition;
        int padding = desiredDecimalPosition - decimalPosition;
        if (padding > 0)
          formattedNum = "        ".substring(8 - padding) + formattedNum;
        return formattedNum;
      }Now it turns out that- despite the fact that I've told DecimalFormat to format precisely 2 digits to the right of the decimal, it sometimes randomly gives me 3, and (less often) 1!
    All this trouble just to try save 16kB of Henrik Bengtsson's sprintf in my jar file...

  • Number pad control

    I was wondering if anybody knew how or has a good numeric pad control or vi. I have a HMI and i need to put up a number pad and haven't quite figured it out. Thanks
    Derek Tucker
    [email protected]

    Well, that makes it even easier. I've done this both with and without an event structure and am regularly using the more recent version (the one with events).
    Build the VI using events to get the key presses and build the string. Once you click your final OK button the string is output through the connector pane to the calling VI. Then you can use this subVI in as many places as you like. If you set the VI to show its FP when called and to be modal the user will have to get rid of this dialog before doing other things.
    I would also suggest adding a Cancel and Backspace buttons to your keypad. It is my experience that they can be helpful.
    To learn more about LabVIEW, I suggest you try searching this site and google for LabVIEW tutorials. Here and here are a couple you can start with and here are some tutorial videos. You can also contact your local NI office and join one of their courses.
    In addition, I suggest you read the LabVIEW style guide and the LabVIEW user manual (Help>>Search the LabVIEW Bookshelf).
    Try to take over the world!

  • HT1212 Hello. I'm having a problem with my mini iPad. When I went to sign on yesterday the number pad moved up and now it is out of site. I can't sign on the only thing that shows is the Apple logo.

    Hello. I'm having a problem signing onto my iPad mini. When I went to sign on yesterday the number pad to sign on moved upward and now it is out of site. Now I can't sign on the only thing that shows is the Apple logo.

    Have you tried to give your iPad a reset? Hold down the sleep and home keys until you see the silver apple. Let it reboot and see if it helps.

  • Is there anyway to have a 10key number pad on my Macbook Pro Retina with Mountain Lion?

    Is there anyway to have a 10key number pad on my Macbook Pro Retina with Mountain Lion?
    I have a separate keyboad that does this for me but I do not want to have to pack it with me on my trip. Please help.

    Just buy a numeric keypad that connects to a usb port. I use a 7+ year old kensington that has a 2 port hub built into it.

  • HT201263 Unable to update, restore or recover. Slider doesn't slide and number pad doesn't work for passcode. What next? Worked last night . Battery charged. When I push Home button I get my backgroung but can't slide slider. Device is recognized by iTune

    Unable to update, restore or recover. Slider doesn't slide and number pad doesn't work for passcode. What next? Worked last night . Battery charged. When I push Home button I get my backgroung but can't slide slider. Device is recognized by iTunes.Help! Thank you.

    Besweet, I'm having the very same problem with 60GB colour which was bought new and worked for 6 months before suddenly just not being recognised by PC. I hadn't changed anything at all.
    I've followed all troubleshooting advice and reinstalled and updated iTunes and iPod updater software but all troubleshooting routes seem to hit the dead end of either the need to access the iPod via the PC (which isn't possible as it's not showing up at all) or the iPod in disc mode stuck at "ok to disconnect".
    It's still under 1yr warranty so will have to take to a dealer.
    I'm sorry that's not much help for you but at least you know it's not a unique problem.
    PC   Windows XP  

  • Apple wired keyboard.  Number pad not working with Windows Office 07 via bo

    I have a 27' Quad core i7 with windows 7 loaded via bootcamp. i have Office 2007 loaded in the Windows partition.
    My problem is that the number pad on my wired apple keyboard does not work in excel or word. The number at the top of the keyboard work fine.
    Has anyone found a solution please.
    Message was edited by: Fernbergjim

    Hi,
    does the numpad work in other Windows programs, or is it just the MS Office programs ?
    Have you tried the 'clear'-key (the one above the '7'-key on the numpad) to enable the numpad ?
    Regards
    Stefan

  • Apple Keyboard with number pad doesn't work with Mac Pro?

    I have a Mac Pro, and have used an Apple Keyboard with a number pad (not bluetooth) with it for quite a while, with no problems. But then my keyboard broke and I ordered a replacement Apple Keyboard with a number pad (not bluetooth). This keyboard doesn't work with my Mac Pro. I have it plugged in, but I can't get the Mac Pro to consistently recognize that there is a USB keyboard connected to it. What magical stuff do I have to do to get the keyboard to be fit for purpose?

    It would not hurt to try an SMC Reset. (But if it does not help, it adds credence to lllaass assertion that the keyboard is not working.)
    Intel-based Macs: Resetting the System Management Controller (SMC) - Apple Support

  • Number to fractional string not working correctly

    I am measuring some parameters from oscillsocpe. i need to write these values to text file for which i am using write to spreadsheet, but what is happening is it is always writing zero value to file, on debugging i found that while converting from number to string it is always writng zero to string indicator, then separately i used that number to fractional string indicator and it is always showing zero in string output , i dont why it is happening.
    Attached below is the code.
    Solved!
    Go to Solution.
    Attachments:
    Time meas sub.vi ‏31 KB

    Ranjeet Singh and Norbert_B
    Thanks for ther reply
    Ranjeet singh,
    number to fractional string is working correctly, there is some other problem, because inly when i am executing this code converting this rise time value to string then only it is showing zero, otherwise for any random number if i am checking this number to fractional string is working correctly.
    Norbert_B
    i dont need to convert it back to string that i am doing only to check why it is showing zero value on converting number to string , because when i was writing to spreadsheet file it was always writing zero value, so when checked that code it was observed that while converting from array of numbers to spreadsheet string it is always writing zero value that's why i am doing it here just to check why it is not converting to string, but your suggestion helped in focussing the string that i am gettong from VISA read, i can use that  in last case if i wont be able to find the solution but main problem is that why i am not able to convert it back to string, why it is showing zero because it is because of this only write to spreadsheet is always showing zero value .

  • Number pad and a few keys jamed. HP 6830s windows 7

    Dont know if right after or later on that i tried doing screen print with the FN key but number pad is jamed as the space bar and main Enter key. 1 = End, 2 =down, 3 =schrol down, 4= going left, 5 =the space bar, 6 =going right, 8=Enter... the rest of the key board ina not affected. If i press Num lock, about the same except that 5=5+ space as the space bar does, 8= 8+ "enter as main Enter key does. Tried to uninstall and reinstall driver of keyboard but no joy. ANy help around?

    Hi Rastapote, and welcome to the HP Support Forums!
    I understand that you are having a problem with the numeric keypad on your HP Compaq 6830s Notebook PC. This is a business Notebook and you would get the best answer to your question on the HP Enterprise Business Community forum as they deal with the Business products.
    I hope this information helps.
    Have a great day!
    I worked on behalf of HP.

  • BUG: Ctrl+Alt+ number to switch modules only works with number pad

    Really annoying, since this worked nicely in the beta. If, for example, you hold down Ctrl+Alt+1 (pressing 1 on the top row of the keyboard) to switch to the Library Module, it doesn't work. If you use 1 on the numeric keypad, it works fine. What's the big deal, you ask? Well, try using the number pad on a laptop - you either get to use the number keys or the regular keys, so this shortcut is essentially useless on anything other than a full-size keyboard.

    Windows treats Ctrl + Alt shortcuts as special. They are used as hotkeys for desktop icons, and if a key is used for that purpose, it overrides any use within a program. I did a little research, and setting a desktop hotkey to 3 will cause your LR symptoms, where you can't used the normal numbers and have to use the keypad to switch modules. On the other hand, you'd probably be seeing the effect of using the non-number-pad keys (they'd launch a program), so it's a mystery.
    In my testing, I noticed that the Firefox browser uses that key combination to switch between its tabbed sessions. If you're running that program, you could test to see if it exhibits the same symptoms.
    Hal

  • I just updated to ios6 and now I have a hard time reading the number pad. Can I get the colors to go back to the way they were before? Or can I get rid of ios6?

    Can I change the colors on the number pad back to what they were before the update?

    Hi Idreier,
    I'd try starting Firefox in [[Safe Mode]]. If you don't have the issue while all of your add-ons, extensions, and themes are disabled, you can try adding them back in one by one until you find the culprit.
    Hopefully this helps!

  • Disfunctional Keyboard Number Pad

    I've been having weird problems with my keyboard's number pad.  Initially, it starts off fine when I boot up the computer.  However, it eventually starts acting funny.  The number keys on it act like num lock is turned off, regardless of it's state.  Even the keys for the math symbols don't work.  They simply don't do anything.  And the plus key acts like the right mouse button in some applications.  My keyboard is the Logitech Wave Keyboard, but I'm doubting that's the problem because it's never acted like this before.  Also, a few weeks ago, the number pad started acting like Num Lock was in the opposite state.

    I haven't seen this problem coming up in a couple days, so I'm guessing it is resolved.  Right now, I can think of two possible causes.  For one, the problem always (or usually) started when I was using OpenOffice.org, which I haven't been using as much.  So it's possibly a bug in OpenOffice.  Also, A few days ago I stopped using evdev as my mouse driver, since it had been causing problems with cycling the input events in /dev/input.  I'm guessing it was more likely to be caused by driver interference.

  • Quality Number Pad

    Does anyone know of a 'quality' number pad for use with a MacBook?
    There seem to be a few wireless number pads out there - but none of them match the Apple build quality / aesthetics. Given the increasing number of 'professionals' using Apple products it seems an obvious ommission from Apple's own product line.
    Just in case anyone from Apple is reading you may want to throw in a few user definable function keys on to any such number pad as any old school PC users would doubtless be keen to have regularly used Excel function keys (F2 for Edit, F4 to fix references, etc - rather than the 'twister' gymnastics needed to perform those functions in Excel for Mac)....

    Is there a reason you don't just by the Aluminum Keyboard? It has a number pad built-in and it's also a very nice full-sized keyboard, which neither of the Mac Laptops have (though the MacBook Pro does come very close). It's also quite handy and allows you to get something like a Griffin Elevator to lift your laptop off the table. Better for your view-angle and better for the laptop's cooling.
    Otherwise, I think the Optimus is selling a number pad, which is rather sexy.

  • "." on the number pad is causing ctrl+alt+del feature

    Hey everyone, I set up VirtualBox for someone in order to run Windows XP about a year ago. I'm not sure what changed but recently when they press the "." key on the number pad, it triggers the ctrl+alt+del feature. Thanks in advance for the help.
    Steven.

    VirtualBox > Preferences > Input might be a place to look.

Maybe you are looking for