How can I format a number with leading zeros?

I need to convert an int to occupy exactly 3 positions in a StringBuffer... I need the space in the StringBuffer to be filled with leading zeros where appropriate. Can anyone show me how to do this correctly please ?
At the moment I am using a pretty dumb workaround to fill the target with "000" first and then parse the int into either 1 2 or 3 positions depending on its value ... it works, but there must be a more correct way to do this ...
Thanks in advance ...

Have you had a look at the class java.text.DecimalFormat? It does exactly what you want... example:
StringBuffer sb = new StringBuffer();
DecimalFormat format = new DecimalFormat("000");
format.format(2, sb, new FieldPosition(0));
System.out.println(sb);
prints out the string 002.

Similar Messages

  • Format a number with leading ones

    How do I format a number with leading ones or twos?
    This is what I'd do to format a number with 8 leading zeroes:
    String.format("%08d", 160)But, if I do the same with 8 leading ones, it does not work the way that I expect:
    String.format("%18d", 160)I'd appreciate your help.
    Thanks.

    933581 wrote:
    >
    What are you expecting?
    >
    I expected that "%18d" would pad with 1s.You assumed
    mmm I just thought that while "%08d" pads with 0s, the same would happen if I used "%18d" to pad with 1s.But if you would have looked at the javadoc of that method you would have very quickly known this was not the case and you wouldn't even had to create this thread. Why operate on guesswork when it is so easy to know reality? :(

  • How can I register a number with iMessage for my iPad2?

    I've got a 3G card with a mobile phone number. How can I register this number with iMessage?

    Good afternoon;
    So to confirm your posting,
    lllaass wrote:
    If it is a number/email address yu are calling:
    - Is it entered in a contact in your Contact app.
    - Does the person have the number/email address registered as their messaging number/emai addrrss.
    the other person needs to register thier information?

  • How can i format an iphone with an differant apple id ?... the phone is locked and it needs the original apple id to activate it which i dont have

    how can i format an iphone with an differant apple id ?... the phone is locked and it needs the original apple id to activate it which i dont have .... help me

    Moh'd . Hassan wrote:
    how can i format an iphone with an differant apple id ?... the phone is locked and it needs the original apple id to activate it which i dont have .... help me
    Then get the previous owner to remove it: Find My iPhone Activation Lock: Removing a device from a previous owner’s account - Apple Support

  • How can I format an HD with System 10.4 and install Leopard Snow?

    How can I format an HD with System 10.4 and install Leopard Snow?

    You need to partion the hard drive using Disk Utility. On one partition install 10.4. On the other 10.6. As a note I trust you are doing this on a MBP and not a PPC Mac because 10.6 won't install.

  • How can I format the number in ?shape-text:

    Hi All,
    I am using shapes in the rtf template. I need to format the number which is used in the shape as
    <?shape-text:mytagname?>
    now mytagname has inconsistent values in the xml like 3, 4.5, 3.55. I want to format this to two digits after decimal i.e 0.00 format.
    How can I format the number in <?shape-text: ???
    Regards,
    Amit Deshpande
    Persistent Systems Ltd

    Hi Amit,
    <?shape-text:format-number(4.5,'#,##0.00')?>

  • Need formula to take text from one cell, add that to a serial number with leading zeros

    I'm trying to create a spreasdheet cell that simplifies creating videotape Numbers from a job name in cell and appends a serial number with leading zeros, and that would survive an export to EXCEL.
    The result would look like
     A Column
    Job Name
    B ColumnTape #
    Formula needed for B
    ClientName-TapeType-
    ClientName-TapeType-001
    =
    ClientName-TapeType-
    ClientName-TapeType-002
    =
    I've tried using a custom cell format that had the text in front of the (Integer) drag and drop, with three integers and leading zeros.
    That worked, but requires recreating the custom cell format for each client and tape type.
    So, I've used the formula =A2& in order to play around with different things, but haven't hit on something that adjusts for increased numbers and retains the leading zeros.
    I don't mind doing a starter for the first row and then adding +1 in the formulas for the rows below.
    I've done that using the custom cell formats for the first B cell and then using =Bx+1 in the following cells.  It works, but I'm hoping to find something simpler or that can be copied and pasted in multiple rows on one paste.
    Thanks in advance.
    Suggestions much appreciated!
    Ted

    Ted,
    This expression in B will give you the pattern I believe you are looking for:
    =A&RIGHT("00"&ROW()-1, 3)
    Here's a screen shot...
    Jerry

  • Re: Material number with leading zeros (40 char long)

    Hello Experts,
    I am working on SAP ME 6.1.4.15.
    I am having a problem with the material number appearing in production order / planned order request document in MEINT and failing there since the material it is looking for is with leading zeros (for example 1291 appearing as 0000000000000000000000000000000000001291) - a total of 40 char long.
    But this material exists in ME as 1291 only. Can any one throw any light on this as to what could be the reason and how to solve it?
    Thanks
    Srinivas

    Dear Srinivas,
    SAP Note 1799009 - 'Presence of leading zeros is inconsistent over IDOC thus SAP MEINT cannot handle all inbound IDOC consistently' should help to resolve the problem.
    Regards,
    Alex.

  • EBS check number with leading zeros

    When we upload EBS, we are getting the check number in the record 16 (reference field). But unlike the check number posted in SAP, the check number in the BAI file has 2 leading zeros. Now since check number is coming in record 16, instead of record 88 we are not able to solve this problem through String configuration.
    So, we thought of following solutions(Options):
    1.    Modify the check lot numbers to have two leading zeros plus associated changes like the form and so on.
    2.    Modify the check number in the incoming file u2013 strip the check number with two leading zeros with the use of customer enhancement. (After upload and before posting)
    Can we solve the problem of leading zeros (before the check number) through any configuration steps instead of changing the SAP script form for check(Check lot) or going for customer enhancement.
    Thanks
    Ron

    You are correct, since the check numbers are coming through only in the 16 records and not in the 88 records, you cannot use a search string to resolve the issue with leading zeros.  Also, I do not know of any other configuration that can be used to reformat the reference values in the 16 records.
    The 2 solutions you have listed are both valid options.  Although with the first option (changing the check lots), you would need to make sure the business users approved of changing the check lots before going forward with that option.  And you would still have to manually clear checks that had been issued prior to the check lot change.
    At my company, we went with the second option - using ABAP code to update the check numbers from the file to match those in SAP (we actually had to add leading zeros to the check numbers).  In our case, we had already created a pre-processor program for other re-formatting of the file that was needed prior to loading it into SAP - so we added the check number logic to that.
    However, if you do not have a pre-processor program, I know there is a user exit available for EBS - exit EXIT_RFEBBU10_001, include ZXF01U01.  I have heard of others using this exit to update the check numbers from 16 records.  The exit is called during the FF.5 processing.
    Regards,
    Shannon

  • Someone had stolen my ipod touch how can retrive my serial number with out the ipod

    someone from my work stole my ipod touch. How can i retrive my serial number but i don't have the ipod anymore

    Report the theft to the police.
    Change your iTunes (Apple ID) password along with any other password that was stored in the iPod.  If any passwords are associated with credit cards, contact the CC company and get your card replaced (with a new number).  If any passwords are associated with your bank or any savings institution, contact them also and discuss approprate action with them.
    The "Find my..." function is pretty much useless if the device is in the hands of a thief.  All that is necessary is for the thief to connect to any computer with iTunes and "Restore as new."
    The only real protection you have is with the personal information on the device rather than the physical device itself.  Something as small as an iPod should have a strong 8-digit (or longer) password AND be configured for automatic wipe in the event of ten consecutive incorrect password entries.

  • How can I make imaginary number with known real number

    I need to compose pure imeginary number in my programming(ie. chamge the real number to be the imeginary part of a complex number). But The functions I found can only change the real number to be the real part of a ccomplex number. How can I compose or make imaginary number with known real number? And Can LABVIEW cope with computation of complex numbers directly?

    Use the Re/Im To Complex function, which is found on the Functions >> Numeric >> Complex palette.
    Good luck,
    Jim

  • How can i format my mac with lion?

    I don't have lion install cd, just snow leopard.
    i want to format my mac.

    Boot from Recovery Partition by holding down Command + R during boot.
    The start Disk Utility from there.
    IMHO I think that the vast majority of the time most reformats are a simple waste of time.
    Allan

  • Auto increment number with leading zeros

    hello,
    i have data column (serial number) that auto generate from script component, i'm using counter +1 and it successful store the number to my db. but the problem is there any idea to store the number to 00001 rather that 1.
    im using this method in my script component
    dim counter as integer = 0
    counter =counter + 1
    row.NoSerial = counter
    TQ

    Why should you send it through the file? WHy not simply create a calculated column based on identity field or sequence based field (if 2012 version and above)  in the destination table and ignore it from the file altogether. ANyways its going to be
    a sequential auotogenerated value isnt it?
    http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server
    If you want to bring it as  00001 etc you need to make it as varchar and use derived column expressions in SSIS as source is text file.
    I would prefer doing this in sqlserver if its just a sequential number and has no direct relationship to the incoming file data fields.
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • HT4993 i got rid of my iphone and now i cant recieve text messages with my new samsung, how can i reset my number so it's not associated with an iphone anymore so i can recieve messages again.

    i got rid of my iphone, and now i cannot recieve text messages from frineds that have iphones, how can i disassociate my number with my old iphone.

    See "Unlink a phone number" toward the bottom here:
    http://support.apple.com/kb/HT5538

  • How can I replace column value with a particular value in SQL

    Hi All,
    Can anyone please tell me how can I format my output with replacing a column value with a specific value which actually depends on the present value of the column
    I am executing the following SQL statement
    select state,count(id) from <table_name> where composite_dn= <composite_dn_name> group by state;
    My Present output is:
    State No.Of Instance
    1 3
    3 28
    I want to replace the value in the column state as follows
    State No.OfInstances
    Completed 3
    Faulted 28
    I want "1" to be reppaced by "Completed" and "3" to be replaced by "Faulted"
    Is is possible with SQL or PL/SQL , if it is then how can I achieve this required result. Please help!!
    Thanks in Advance!!
    Edited by: Roshni Shankar on Oct 27, 2012 12:38 AM

    Hi Roshni,
    I guess this CASE clause can be simulated by a DECODE and also it is very easy to use.
    Refer -- http://www.techonthenet.com/oracle/functions/decode.php
    select decode(t1.state,t2.state_id,t2.state_name), t1.count_id
    from <table_2> t2, (select state,count(id) count_id
    from <table_name>
    where composite_dn= <composite_dn_name>
    group by state) t1
    where t1.state = t2.state_id;HTH
    Ranit B.
    Edited by: ranit B on Oct 27, 2012 2:02 PM
    -- link added
    Edited by: ranit B on Oct 27, 2012 2:19 PM
    -- sample code added

Maybe you are looking for

  • Unable to delete entries from queue (SMQ2)

    Hi I am unable to delete entries from inbound queue due to number of entries are too high (Approximately 160,000). Deletion in dialog mode producing timeout error...Is there any way to delete these entris in background mode or any other idea about th

  • All of a sudden, my MacBook Pro is running very slow?!?

    I have had my MacBook Pro for a about a year and a half and have had NO problems with it. A few weeks ago, my MacBook has become very slow, to the point where I have to restart the computer. Most of the time the dreaded beach ball appears and my MacB

  • Struggling to migrate sharepoint sites developed on dev or qa over to production

    So, user 1 comes along and wants to put together a new site. He wants to use SharePoint Designer to set up pretty colors, create a new theme and use it, create a series of custom lists, etc. Meanwhile, on these same machines, there is a variety of In

  • To calculate phase in speedy 33

    Hi , I have designed a system with a transmitter portion and a receiever portion for communication with a speedy 33 module . In the transmitter part , i have generated a sine waveform which varies between two phases , 0 and 180 . Now in the receiver

  • How do I transfer text messages from samsung phone

    Good morning, I use my Samsung Strive phone for business texting.  When I get a "msilbox full" message, I want to download those messages to my iMac.  Can someone tell me how to do this--or point me to the solution? Thanks! John