Java Integer Format formatInt(int in, int precision)

Here you can find the source of formatInt(int in, int precision)

Description

convert a double into a String with a given precision default double formatting is not very pretty

License

Apache License

Parameter

Parameter Description
in int to convert

Return

non-null formatted string

Declaration

public static String formatInt(int in, int precision) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*ww w . j a  v  a2  s  . c  o m*/
     * convert a double into a String with a given precision
     * default double formatting is not very pretty
     * @param in int to convert
     * @param int positive precision
     * @return non-null formatted string
     */
    public static String formatInt(int in, int precision) {
        String ret = Integer.toString(in);
        if (ret.length() > precision)
            throw new IllegalArgumentException("Cannot write " + in + " in " + precision + " digits");
        while (ret.length() < precision)
            ret = "0" + ret;
        return (ret);
    }

    /**{ method
     @name toString
     @function convert a char to a string
     @param c the char
     @return the String
     }*/
    public static String toString(char c) {
        StringBuffer s = new StringBuffer();
        s.append(c);
        return (s.toString());
    }
}

Related

  1. format(int num)
  2. format(int num, int length)
  3. format(int spaces, String string)
  4. format(int ticks)
  5. formatInt(int i, int radix, int len)
  6. formatInt(int myint, int maxint)
  7. formatINT(int n)
  8. formatInt(int n)
  9. formatInt(int value, int length)