假设我们有数字 300,我希望它被填充以 300.000 结尾
或者数字 23,5 类似于 23.500
希望对您有所帮助,详细信息在评论中。
function formating(n) {
// Convert number into a String
const str = n.toString();
// Find the position of the dot.
const dotPosition = str.search(/./);
// Return string with the pad.
if (dotPosition === -1) {
return str += ".000";
} else {
const [part1, part2] = str.split('.');
return part1 + '.' + part2.padEnd(3, "0");
}
}