Takes in an integer, and returns a String that adds commas to the appropriate locations within the number. - Java java.lang

Java examples for java.lang:int Format

Description

Takes in an integer, and returns a String that adds commas to the appropriate locations within the number.

Demo Code


//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) {
        int number = 42;
        System.out.println(formatNumber(number));
    }//from  w w w .j a v a  2 s  . c om

    /**
     * Takes in an integer,
     *
     * @number, and returns a String that adds commas to the appropriate locations within the number.
     * @param number
     * @return
     */
    public static String formatNumber(int number) {
        DecimalFormat formatter = new DecimalFormat("#,###");
        return formatter.format(number);
    }
}

Related Tutorials