Javascript nodej介绍如何在数字的末尾填充。零呢?

Javascript nodej介绍如何在数字的末尾填充。零呢?,javascript,node.js,Javascript,Node.js,假设我们有数字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 pa

假设我们有数字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");
 }
}

这回答了你的问题吗?您可以使用DigitalJS在数字周围玩: