Format int To String - Java java.lang

Java examples for java.lang:int

Description

Format int To String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int val = 2;
        int width = 2;
        System.out.println(intToString(val, width));
    }/*from w  w  w  . j  av  a 2  s.  c o m*/

    static String mSpaces = "                                ";

    public static String intToString(int val, int width) {
        String str = new String("" + val);
        if (str.length() >= width) {
            return str;
        }
        return mSpaces.substring(0, width - str.length()) + str;
    }
}

Related Tutorials