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...

Similar Messages

  • Formatting a number element with commas and decimals.

    I have a dataset with numbers.  I already set the column type to "number".
    I want to format the number data with commas and decimals.  I found a way to add decimals, but I'm still very confused how to do commas and to combine that with the decimal formatting.
    Also, how do I apply that to a repeat region in my spry region?
    Any help would be appreciated.  Thank you very much.

    I found this on one of the forum questions.
    Excel dont preserve the trailing zeros.
    try opening the xcel and type 5.0000 and tab out, it wont preserve the trailing zeros after decimal.
    try finding out how to disable the option in excel. if you do that, you will see what you send out from the report.
    But you can try to convert the number into a text by trying to append a space at the beginning. See if that would work.
    Thanks
    Swarna

  • Keyboard glitch--number pad with SL

    One computer, two drives with two different OSX versions; one Leopard and one Snow Leopard. It keeps me from having all my eggs in one basket.
    One keyboard. The extended aluminum slab.
    Booting in SL causes the number pad's numbers to fail to work. Booting in 10.5 Leopard has them working just fine.
    Before installing SL over the previous OSX, they worked fine. All the other keys work just fine in both versions.
    I've tried to find anything similar on this in the discussions, but either nothing's there or I missed it. How can I get my keyboard functional in SL?

    I don't know if SL boots up with that check box checked as the default or what. Maybe I was fooling with it months ago and the SL install simply inherited it.
    Wouldn't it be nice if potentially mind-warping settings like this were the subject of a poster when SL booted up from a restart?
    "Here are a bunch of Gotchas you may have implemented:
    • Your Mouse Keys feature is ON. This may drive you stupid!
    • You have shut OFF your Wi Fi. Good luck with that!
    • Did you know you have set in motion the mother of all
    time consuming memory tests? Would you like us to stop it?"

  • Need help in formatting a number field with same precision for excel output

    I am trying to get the same preciision for a field in RTF template when the output is in EXCEL
    i.e. same number of digits after decimal. So, I would like to show 150 as 150.00 and 123.2 as 123.20 etc !! Is there a way to do it. the Built in formatting will not display 150.00 for 150 . but if i add $ symbol it will display. But i dont need $ syambol. please help.

    I found this on one of the forum questions.
    Excel dont preserve the trailing zeros.
    try opening the xcel and type 5.0000 and tab out, it wont preserve the trailing zeros after decimal.
    try finding out how to disable the option in excel. if you do that, you will see what you send out from the report.
    But you can try to convert the number into a text by trying to append a space at the beginning. See if that would work.
    Thanks
    Swarna

  • Formatting--Replacing 0.00 with SPACES??

    Hi Experts,
    my_Discount LIKE BSEG-KBETR (CURR field i.e. DEC field).
    Am doing grand totals of my_classical report, which contains some amounts and Discount% (but, am not doing Total of Discount%).
    But, am getting 0.00 under the column of Disc% in Grand total row!
    So, I dont wanna even to display the 0.00 numbers, instead I wanna show SPACE, in the said column!
    So, Is it possible?
    thanq.

    KBETR is the currency field,so if no value then it displays   like 0.00
    if you are working on report,when you have output internal table,declare kbetr as char data type.
    move the value from curr value to char data type.
    if kbetr = '0.00'.
    kbeter = space.
    endif.
    You have to declare char data type,you can't pass space to currency data type.
    Thanks
    Seshu

  • IMac, 2.5-- Mtn lion,  -- number pad non responsive

    With my four month old, iMac 2.5,  I suddenly found that I was unable to use the number pad with certain dialog boxes in Photoshop, notably the image size dialog box.  I trahsed prefs, restarted, etc.  No luck.  Adobe boards suggested that it was a failed keyboard.  I exchanged it at an Apple store today.........same problem.
    I notice that I can't get the keys to repeat, either, nor can I exit from certain apps using  the enter key....
    I'd be most grateful for any insight

    Intel-based Macs: Using Apple Hardware Test

  • How do I set the number pad to work with spaces?

    It has worked with other OSs but with Mavericks 10.9.5 (and from what I here, Yosemite) you can not use the number pad to change spaces. I know I can use the numbers across the top or the arrows, I just find it quicker and ergonomic to use the number pad. As many spaces I use this is very inconvenient to be reaching across the keyboard. Is there a way to fix this. I went to the preferences but could not find anything there to change. Maybe Apple could fix this in there next update.

    Apple doesn’t routinely monitor the discussions. These are mostly user to user discussions.
    Send Apple feedback. They won't answer, but at least will know there is a problem. If enough people send feedback, it may get the problem solved sooner.
    Feedback

  • Padding String with spaces -tried known methods but code not working!

    I am trying to figure out an easy way to pad a string with spaces at the end. The way that definitely works is a for loop where string = string + " "; until string is the length that I need. However, for several dozen strings this is a lot of unnecessary overhead. I've done some research and tried a few different things - yet none of them seem to actually pad the string.
    import org.apache.commons.lang.StringUtils;
    public class Test
         public static void main(String[] args)
              String one = "1234";
                    //print string at current lenght
              System.out.println(one+"*");
                    //try padding using String.format (in padRight method below)
              one = padRight(one,(8-one.length()));
              System.out.println(one+"*");
                    one="1234";
                    //try padding using StringUtils
              String tmp = StringUtils.rightPad(one, (8-one.length()));
              System.out.println(tmp+"*");
         private static String padRight(String s, int n)
              return String.format("%1$-" + n + "s", s);
    }I've researched and these are the two most common methods people are suggesting. I am using java 1.5 so the String.format should work.. however, when I run it (I use JBuilder) the output is always "1234*".... after I try to pad it the output should be "1234 *" (with 4 spaces between the 4 and *) . Any suggestions is greatly appreciated! Thanks in advance.

    Wow, I completely botched that... uncle_alice, thank you for clearing that up for me.
    pbrockway2 - thank you, I actually did peruse the Formatter documentation - I would never post without researching something first... but I must have read it with one eye shut because walked away with the impression that I needed to submit the difference. I obviously misunderstood the documentation.
    Thanks!

  • 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? :(

  • 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.

  • Error in query " Value '' of characteristic  is not a number with  spaces "

    Hi Gurus,
    I am facing an error in a query with the description:
    Error
    Value '' of characteristic  is not a number with  spaces
    System error in program SAPLRRK0 and form RSRDR;SRRK0F30-01-
    Description
    This error is occuring in Production system only, the report is running fine in Development environment.
    The error is also visible when I execute it in RSRT, I am not able to find the cause of error through RSRT.
    Can anyone please help me with the possible solutions.
    Thanks & Regards,
    Shreyas

    Hi Juergen,
    thanks for the input, I tried to run the report after loading the master data but still there was no improvement.
    I am not sure why this kind of problem is rising, some of the similar reports on same infoprovider are running fine.
    Please let me know if you know anything about the cause of error.
    Thanks & Regards,
    Shreyas

  • Export Excel Table in .txt File with space delimited text in UNICODE Format

    Hi all
    I've a big unsolved problem: I would like to convert an Excel table with some laboratory data in it (descriptions as text, numbers, variables with some GREEK LETTERS, ...). The output should be a formatted text with a clear structure. A very good solution is
    given by the converter in Excel "Save As" .prn File. All works fine, the formattation is perfect (it does not matter if some parts are cutted because are too long), but unfortunately the greek letters are converted into "?"!!!
    I've tried to convert my .xlsx File in .txt File with formatting Unicode and the greek letters are still there! But in this case the format is not good, the structure of a table is gone!
    Do you know how to save an Excel file in .prn but with Unicode formatting instead of ANSI or a .txt with space delimited text?
    Thanks a lot to everyone that can help me!
    M.L.C.

    This solution works in Excel/Access 2013.
    Link the Excel table into Access.
    In Access, right-click the linked table in the Navigation Pane, point your mouse cursor to "Export", and then choose "Text File" in the sub-menu.
    Name the file, and then under "Specify export options", check "Export data with formatting and layout".  Click "OK".
    Choose either Unicode or Unicode (UTF-8) encoding.  Click "OK".
    Click "Close" to complete the export operation.

  • 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

  • Apple Keyboard with number pad the V key will not work

    Apple Keyboard with number pad the V key will not work.  Everyother key works but not the V.  I have a MacBook Pro.  Any suggestions?

    Call AppleCare and get it replace if it's under warranty; otherwise, get a new one. They're only $49 USD.

Maybe you are looking for