// CommaFormatter.java
import java.text.DecimalFormat;
/**
数値にカンマ付けのフォーマットを行うクラス
*/
public class CommaFormatter
{
private int commaInterval;
/**
* カンマ付けを行う桁数を指定して構築
*/
public CommaFormatter(int commaInterval)
{
this.commaInterval = commaInterval;
}
/**
* 数値にカンマ付けのフォーマットを行う
*
* @param value フォーマット対象の数値
* @return カンマ付けを行った数値文字列。<br>0以下のcommaIntervalが指定された場合は、そのままの文字列に変換したものを返す。
*/
public String format(long value)
{
String result = "";
if(commaInterval <= 0)
{
result = Long.toString(value);
}
else
{
DecimalFormat formatter = new DecimalFormat(createFormatString(commaInterval));
result = formatter.format(value);
}
return result;
}
private String createFormatString(int commaInterval)
{
StringBuffer strFormatBase = new StringBuffer("#,");
for(int i=0; i<commaInterval; i++)
{
strFormatBase.append("#");
}
return strFormatBase.toString();
}
}
Source is here. (LZH Format, 580Byte, Shift-JIS)
ここで、フォーマット文字列中の、カンマのあとの#の数によって、
何桁ごとにカンマを付けるかを制御することができます。
つまり、3桁ならば、「#,###」という風になるわけです。
このことを考慮してプログラムを読めば、すぐに分かると思います。