`
20386053
  • 浏览: 432835 次
文章分类
社区版块
存档分类
最新评论

java实现数字千分位的显示

 
阅读更多

由于项目中要求输入的数字用千分位显示,数字保留两位小数,而且要求再删除数字的时候也要求删除后的数字也要是千分位显示,好像表达的有点不清楚,贴代码吧,作为一个小工具吧。

	/**
	 * 格式化数字为千分位显示;
	 * @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);
	}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics