數字顯示會有千分位的需求,比如10000顯示為10,000
前端JavaScript分隔千分位方式如下,先把小數排除再加回來:
function toCurrency(num){
var parts = num.toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
}
Java的話,用到更複雜的Pattern: (?<=\\d)(?=(?:\\d{3})+$)
s.replaceAll("(?<=\\d)(?=(?:\\d{3})+$)", ",");