1. 百分数:
Java Code:
double value = (double) 3 / 7; ----- 必须是double类型,/才会有整数和小数,int类型,只有整数部分。
DecimalFormat format = new DecimalFormat("0.00%");
String str = format.format(value);
System.out.println(str);
结果:42.86%
Js Code:
var a=0.324467;
var b = (a * 100).toFixed(2) + "%";
alert(b);
结果:32.45%
以上结果在保留小数位数时都会四舍五入后(注意:3/7 要强制转换成double类型,否则3/7=0.0 )
2. 千分位,如1,234.67
java:0表示该位没有则显示0;#表示该位没有则不显示。
public static String parseMoney(BigDecimal d) {
DecimalFormat df = new DecimalFormat("###,##0.00");
return df.format(d);
}
js:
//num为整数,无小数位
function toThousands(num) {
return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}
//str任意数
function regix(str){
var num = parseFloat(str).toFixed(3);//两位小数
var s = num.substring(0, (num.length-1));//只取小数位2位
return s && s.toString().replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {
return $1 + ",";
});;
}
3.金额转换,分 to 元
java:
String num = String.format("%.02f元", (double)1555/100);
js:
var money = (1555/ 100).toFixed(2) + '';