Java Zero Format zerofill(int x, int desiredWidth)

Here you can find the source of zerofill(int x, int desiredWidth)

Description

fills the left side of a number with zeros
e.g.

License

Open Source License

Declaration

public static String zerofill(int x, int desiredWidth) 

Method Source Code

//package com.java2s;
/*/* ww  w  .  j  ava2s .  c  om*/
 * ====================================================================
 * Copyright (c) 1995-1999 Purple Technology, Inc. All rights
 * reserved.
 *
 * PLAIN LANGUAGE LICENSE: Do whatever you like with this code, free
 * of charge, just give credit where credit is due. If you improve it,
 * please send your improvements to alex@purpletech.com. Check
 * http://www.purpletech.com/code/ for the latest version and news.
 *
 * LEGAL LANGUAGE LICENSE: Redistribution and use in source and binary
 * forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * 3. The names of the authors and the names "Purple Technology,"
 * "Purple Server" and "Purple Chat" must not be used to endorse or
 * promote products derived from this software without prior written
 * permission. For written permission, please contact
 * server@purpletech.com.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND PURPLE TECHNOLOGY ``AS
 * IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * AUTHORS OR PURPLE TECHNOLOGY BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 *
 **/

public class Main {
    /**
     * fills the left side of a number with zeros <br>
     * e.g. zerofill(14, 3) -> "014" <br>
     * e.g. zerofill(187, 6) -> "000014" <br>
     * e.g. zerofill(-33, 4) -> "-033" <br>
     **/
    public static String zerofill(int x, int desiredWidth) {
        StringBuffer buf = new StringBuffer();
        if (x < 0) {
            buf.append("-");
            desiredWidth--;
            x = -x;
        }
        while (desiredWidth > 7) {
            buf.append("0");
            desiredWidth--;
        }
        switch (desiredWidth) {
        case 7:
            if (x < 1000000)
                buf.append("0");
        case 6:
            if (x < 100000)
                buf.append("0");
        case 5:
            if (x < 10000)
                buf.append("0");
        case 4:
            if (x < 1000)
                buf.append("0");
        case 3:
            if (x < 100)
                buf.append("0");
        case 2:
            if (x < 10)
                buf.append("0");
        }
        buf.append(x);
        return buf.toString();
    }
}

Related

  1. zeroExtend(long value, int inputBits)
  2. zeroFill(float[] array)
  3. zeroFill(int iv, int len)
  4. zeroFill(int num, int size, String character)
  5. zeroFill(int value, int fieldWidth)
  6. zeroFillString(String originalString, int numZeros)
  7. zeroFormat(Integer source)
  8. zeroFormattedStr(int number, int length)
  9. zeroIfNull(Integer i)