NodeJS如何用来填充数字的末尾。和零? [重复]

问题描述 投票:-3回答:1
假设我们有300,我希望将其填充为300.000

或者数字23,5应该是23.500

javascript node.js
1个回答
0
投票
希望它会有所帮助,详细信息在评论中。

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"); } }


0
投票

希望它会有所帮助,详细信息在评论中。

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");
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.