[JavaScript] ๊ธ์ก ๋ณํ - ์ซ์์์ ํ๊ธ๋ก, ํ๊ธ์์ ์ซ์๋ก
2024. 1. 22. 01:30
ํ๊ธ -> ์ซ์
function korToNum(value) {
var numKor = '์์ผ์ด์ผ์ฌ์ค์ก์น ํ๊ตฌ';
var numUnit1 = { '์ญ': 10, '๋ฐฑ': 100, '์ฒ': 1000 };
var numUnit2 = { '๋ง': 10000, '์ต': 100000000, '์กฐ': 1000000000000 };
var num = 0;
var ret = 0;
var total = 0;
for (var i = 0; i < value.length; i++) {
var n = value.charAt(i);
if (numKor.indexOf(n) != -1) {
num = numKor.indexOf(n);
}
else if (n in numUnit1) {
if (num == 0) num = 1;
ret += num + numUnit1[n];
}
else if (n in numUnit2) {
if (ret == 0 && num == 0) return "";
total += (ret + num) * numUnit2[n];
ret = 0;
num = 0;
}
else return "";
}
total += ret + num;
return total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
์ซ์ -> ํ๊ธ
function numToKor(value) {
value = value.replace(/[^0-9]/g, '');
var numKor = '์์ผ์ด์ผ์ฌ์ค์ก์น ํ๊ตฌ';
var numUnit1 = ' ์ญ๋ฐฑ์ฒ';
var numUnit2 = ' ๋ง์ต์กฐ';
var num = "";
var ret = "";
var total = "";
var j = 0;
while (Value > 0) {
num = Number(value) % 10000;
value = parseInt(Number(value) / 10000);
for (var i = 0; i < 4; i++) {
if (num % 10 != 0)
ret = numKor.charAt(num % 10) + numUnit1.charAt(i) + ret;
num = parseInt(num / 10);
}
if (ret != "")
total = ret + numUnit2.charAt(j) + total;
ret = "";
j++;
}
return total.replace(/ /g, "");
}
728x90
'๐ฃ์ธ์ด > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] Query String (0) | 2022.03.23 |
---|---|
[JavaScript] OMDb API : ์ํ์ ๋ณด ๋ฐ์์ค๊ธฐ (0) | 2022.03.23 |
[JavaScript] Storage : localStorage, SessionStorage (0) | 2022.03.23 |
[JavaScript] Lodash (0) | 2022.03.23 |
[JavaScript] ๋ฐ์ดํฐ ๋ถ๋ณ์ฑ, ์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ (0) | 2022.03.23 |