Money Format Conversion Functions

// coin format conversion functions
// incoming string of numbers, is converted into the form of a comma-delimited with
// 12345 → 12,345 e.g.
let readline = require('readline-sync');
the console.log ( "Please enter the number to be converted: ');
let money = readline.question (); // Input example: 12345
function change(money) {
let str = "";
// from the right of the first start is calculated from right to left
for (let i = money.length - 1; i >= 0; i--) {
str += money[money.length - 1 - i];
if (i % 3 == 0 && i != 0) {
str += ",";
}
}
return str;
}
console.log(change(money));
// 1 2 3 4 5
// 1 2,3 4 5

Guess you like

Origin www.cnblogs.com/dbda/p/11407319.html