Stores given integer value as string into given stringbuffer. - Java java.lang

Java examples for java.lang:StringBuffer

Description

Stores given integer value as string into given stringbuffer.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        StringBuffer buffer = new StringBuffer();
        int value = 2;
        int minSize = 2;
        append(buffer, value, minSize);//from ww w .j  ava 2s  . c o m
    }

    /**
     * Stores given integer value as string into given string buffer.
     * @param buffer the string buffer.
     * @param value the integer.
     * @param minSize count of characters to reserve for value (it this value is 2, then int value 1 become "02" etc).
     */
    public static void append(StringBuffer buffer, int value, int minSize) {
        final String valueAsString = String.valueOf(value);

        final int padding = minSize - valueAsString.length();

        for (int i = 0; i < padding; i++) {
            buffer.append("0");
        }

        buffer.append(valueAsString);
    }
}

Related Tutorials