• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Java: Format Integer Into Fixed Width String

Java: Format Integer Into Fixed Width String

January 5, 2010 by Sze Hau 2 Comments

Sometime, you may want to format an integer or a long value into a fixed width String. You need to display the value in fixed width especially in reports where you want to keep the numbers in aligned (e.g. 3456 as 0003456 and 1234 as 0001234).

Other than that, you may also want to format the integer value for a better visibility. For example, to display integer value as a document number (e.g. 12345678 as 00-01234-5678), for example an account number.

The Java.format() is not flexible enough to solve my problem. At least it cannot be used to format a fixed length document number in the format I want: XX-XXXX-XXXX. Therefore I decide write my own format function.

The following function format a integer value into a fixed length String.

public class IntegerUtil {
    public static String formatFixedWidth(int value, int width) {
        char[] chars;
        int i;
        int c;

        chars = new char[width];
        for (i = 0; i < width; i++) {
            c = value % 10;
            chars[width-i-1] = (char)('0' + c);
            value = value / 10;
        }
        return new String(chars);
    }
}

To format the document number like the one I said above, you simply modify the source code above to add one or two lines code to append a “-” to the String when the loop iterate to the place where you want to put the “-“. Remember to increase the characters buffer as well.

The following function shows a sample implementation of my document number format.

public class IntegerUtil {
  public static String formatDocumentNumber(int value) {
    char[] chars;
    int i;
    int c;
    int size;

    size = 10+2;
    chars = new char[size];
    for (i = 0; i < size; i++) {
      if (i == 4 || i == 9) {
        chars[width-i-1] = '-';
        continue;
      }
      c = value % 10;
      chars[width-i-1] = (char)('0' + c);
      value = value / 10;
    }
    return new String(chars);
  }
}

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: format, format Integer, how to, Java, performance, String

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Comments

  1. Bowmessage says

    May 13, 2010 at 8:34 am

    Using Format is a good solution for some leading 0s! Just format with the String “%05d”, 5 being the fixed-width of the String representation.

  2. szehau says

    May 13, 2010 at 10:36 am

    Hi, you can test the Java String.format againts my function, you will see my code is faster. Second my code is more flexible. I can format an integer value (e.g. 1234567) into the format I want (e.g. 12-34-567).

Leave a ReplyCancel reply

Advertisement

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

advertisement blog C# Chrome DecimalFormat EGPC Float format format Integer free icons Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL site value spam SQL static constructor String thread tiny URL Tips and Tricks trackback twitter video Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in