• 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);
  }
}

More from my site

  • Java: Format Long Integer Into Hexadecimal StringJava: Format Long Integer Into Hexadecimal String
  • Java: Format integer into number of decimal places and performanceJava: Format integer into number of decimal places and performance
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: Loading Large Data into JTable or JListJava: Loading Large Data into JTable or JList
  • Java: Randomly sort values in a array (the generic way) in single passJava: Randomly sort values in a array (the generic way) in single pass
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer

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 Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

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

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

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