JavaBean实例8:将长整型的数字分位显示
有时候我们为了用户方便查看某一数字,我们常常用分位显示,就是每隔几个数字用逗号隔开。
我们可以应用StringBuffer类中的方法实现:(我们习惯从后面开始分位)
1.insert();
该方法可以动态地向一个StringBuffer对象类型的字符串中指定位置插入指定字符串。
2.reverse();
该方法可以将字符串反转。
代码实现如下:
1.JavaBean:
StringUtil.java
package exa134;
public class StringUtil {
private long longValue; //要分位的数字
private int digit; //分位位数
private String formatStr; //分位后的字符串
public long getLongValue() {
return longValue;
}
public void setLongValue(long longValue) {
this.longValue = longValue;
}
public int getDigit() {
return digit;
}
public void setDigit(int digit) {
this.digit = digit;
}
public String getFormatStr() {
//将long类型的值转换为可动态修改的StringBuffer对象
StringBuffer sb = new StringBuffer(String.valueOf(longValue));
sb =sb.

浙公网安备 33010602011771号