由于项目中要求输入的数字用千分位显示,数字保留两位小数,而且要求再删除数字的时候也要求删除后的数字也要是千分位显示,好像表达的有点不清楚,贴代码吧,作为一个小工具吧。
/**
* 格式化数字为千分位显示;
* @param 要格式化的数字;
* @return
*/
public static String fmtMicrometer(String text)
{
DecimalFormat df = null;
if(text.indexOf(".") > 0)
{
if(text.length() - text.indexOf(".")-1 == 0)
{
df = new DecimalFormat("###,##0.");
}else if(text.length() - text.indexOf(".")-1 == 1)
{
df = new DecimalFormat("###,##0.0");
}else
{
df = new DecimalFormat("###,##0.00");
}
}else
{
df = new DecimalFormat("###,##0");
}
double number = 0.0;
try {
number = Double.parseDouble(text);
} catch (Exception e) {
number = 0.0;
}
return df.format(number);
}
本文介绍了一种将数字格式化为千分位显示的方法,并确保数字保留两位小数。该方法适用于项目中需要对数字进行特定格式化的需求。
2688

被折叠的 条评论
为什么被折叠?



