Java Number to String numberToString(int number, int length, String type, int location)

Here you can find the source of numberToString(int number, int length, String type, int location)

Description

number To String

License

Open Source License

Declaration

public final static String numberToString(int number, int length, String type, int location) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public final static String numberToString(int number, int length, String type, int location) {

        String numberStr = String.valueOf(number);

        if (numberStr.length() == length) {
            return numberStr;
        } else if (numberStr.length() > length) {
            return numberStr.substring(numberStr.length() - length);
        }// w ww  .  j a v a2s .c om

        StringBuffer value = new StringBuffer();
        for (int i = 0; i < length - numberStr.length(); i++) {
            value.append(type);
        }
        String resStr;
        if (location == 1) {
            resStr = numberStr + value.toString();
        } else {
            resStr = value.append(numberStr).toString();
        }
        return resStr;
    }

    public final static String numberToString(int number, int length) {

        String numberStr = String.valueOf(number);

        if (numberStr.length() == length) {
            return numberStr;
        } else if (numberStr.length() > length) {
            return numberStr.substring(numberStr.length() - length);
        }

        StringBuffer value = new StringBuffer();
        for (int i = 0; i < length - numberStr.length(); i++) {
            value.append("0");
        }
        value.append(numberStr);
        return value.toString();
    }
}

Related

  1. numberToString(double num, int digits)
  2. numberToString(int gmNumber)
  3. numberToString(Number number)
  4. numberToString(Object instance)
  5. numberToString(Object num)
  6. numberToStringFlushLeft(long number, int width)