如何在 JavaScript 中显示带有千位逗号分隔符的数字而不消失?
我正在制作货币兑换计算器。 HTML 中的输入类型是“文本” 像这样。
我尝试将输入值转换为带有数千个逗号分隔符,例如 fromInput.value = amount.toLocaleString("en-US");
但存在一个问题,当输入的数字超过千个单位时,数字就会消失。 如何显示带有数千个逗号分隔符的数字而不使数字消失?
function convert(type) {
let amount = 0;
if (type == "from") {
amount = parseFloat(fromInput.value); // number
let convertedAmount = amount * currencyRatio[fromCurrency][toCurrency]; //number
toInput.value = convertedAmount.toLocaleString("en-US"); //string
fromInput.value = amount.toLocaleString("en-US"); //string
} else {
amount = parseFloat(toInput.value);
let convertedAmount = amount * currencyRatio[toCurrency][fromCurrency];
fromInput.value = convertedAmount;
}
}
这是完整的 JS 代码
let currencyRatio = {
USD: {
KRW: 1227.6,
USD: 1,
GBP: 0.76,
EUR: 0.92,
unit: "Dollar",
},
KRW: {
KRW: 1,
USD: 0.00081,
GBP: 0.00062,
EUR: 0.00074,
unit: "Won",
},
GBP: {
KRW: 1620.47,
USD: 1.32,
GBP: 1,
EUR: 1.21,
unit: "Pound",
},
EUR: {
KRW: 1343.13,
USD: 1.09,
GBP: 0.83,
EUR: 1,
unit: "Euro",
},
};
const fromBtn = document.querySelector("#from-button");
const fromList = document.querySelectorAll("#from-currency-list a");
const toBtn = document.querySelector("#to-button");
const toList = document.querySelectorAll("#to-currency-list a");
const fromInput = document.querySelector("#from-input");
const fromInputDisplay = document.querySelector("#from-input-display");
const toInput = document.querySelector("#to-input");
const toInputDisplay = document.querySelector("#to-input-display");
let fromCurrency = "USD";
let toCurrency = "USD";
fromList.forEach((item) => {
item.addEventListener("click", () => {
fromBtn.innerText = item.innerText;
fromCurrency = item.innerText;
fromInputDisplay.innerText = currencyRatio[item.innerText]["unit"];
convert("from");
});
});
toList.forEach((item) => {
item.addEventListener("click", () => {
toBtn.innerText = item.innerText;
toCurrency = item.innerText;
toInputDisplay.innerText = currencyRatio[item.innerText]["unit"];
convert("from");
});
});
function convert(type) {
let amount = 0;
if (type == "from") {
amount = parseFloat(fromInput.value); // number
let convertedAmount = amount * currencyRatio[fromCurrency][toCurrency]; //number
toInput.value = convertedAmount.toLocaleString("en-US"); //string
fromInput.value = amount.toLocaleString("en-US"); //string
} else {
amount = parseFloat(toInput.value);
let convertedAmount = amount * currencyRatio[toCurrency][fromCurrency];
fromInput.value = convertedAmount;
}
}
fromInput.addEventListener("keyup", () => {
convert("from");
});
fromInput.addEventListener("click", () => {
fromInput.value = "";
});
toInput.addEventListener("keyup", () => {
convert("to");
});
toInput.addEventListener("click", () => {
toInput.value = "";
});
这是完整的 HTML 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Currency Exchange</title>
<link rel="stylesheet" href="style.css" class="rel" />
</head>
<body>
<div class="exchange-box">
<div class="dropdown">
<button class="dropbtn" id="from-button">USD</button>
<div class="dropdown-content" id="from-currency-list">
<a href="#">USD</a>
<a href="#">KRW</a>
<a href="#">GBP</a>
<a href="#">EUR</a>
</div>
</div>
<div class="input-area">
<input type="text" value="0.00" id="from-input" />
<div class="display" id="from-input-display">dollar</div>
</div>
</div>
<h1>=</h1>
<div class="exchange-box">
<div class="dropdown">
<button class="dropbtn" id="to-button">USD</button>
<div class="dropdown-content" id="to-currency-list">
<a href="#">USD</a>
<a href="#">KRW</a>
<a href="#">GBP</a>
<a href="#">EUR</a>
</div>
</div>
<div class="input-area">
<input type="text" value="0.00" id="to-input" />
<div class="display" id="to-input-display">dollar</div>
</div>
</div>
<script src="main2.js"></script>
</body>
</html>
i'm making currency changing calculator. input type in HTML is 'text'<input type="text" value="0.00" id="from-input" />
like this.
i've tried converting input value to with thousands comma separators likefromInput.value = amount.toLocaleString("en-US");
but there is a problem that the number disappears when the number exceeding the thousand units is entered.
How can i display the number with thousands comma separators without number disappearing?
function convert(type) {
let amount = 0;
if (type == "from") {
amount = parseFloat(fromInput.value); // number
let convertedAmount = amount * currencyRatio[fromCurrency][toCurrency]; //number
toInput.value = convertedAmount.toLocaleString("en-US"); //string
fromInput.value = amount.toLocaleString("en-US"); //string
} else {
amount = parseFloat(toInput.value);
let convertedAmount = amount * currencyRatio[toCurrency][fromCurrency];
fromInput.value = convertedAmount;
}
}
this is whole JS code
let currencyRatio = {
USD: {
KRW: 1227.6,
USD: 1,
GBP: 0.76,
EUR: 0.92,
unit: "Dollar",
},
KRW: {
KRW: 1,
USD: 0.00081,
GBP: 0.00062,
EUR: 0.00074,
unit: "Won",
},
GBP: {
KRW: 1620.47,
USD: 1.32,
GBP: 1,
EUR: 1.21,
unit: "Pound",
},
EUR: {
KRW: 1343.13,
USD: 1.09,
GBP: 0.83,
EUR: 1,
unit: "Euro",
},
};
const fromBtn = document.querySelector("#from-button");
const fromList = document.querySelectorAll("#from-currency-list a");
const toBtn = document.querySelector("#to-button");
const toList = document.querySelectorAll("#to-currency-list a");
const fromInput = document.querySelector("#from-input");
const fromInputDisplay = document.querySelector("#from-input-display");
const toInput = document.querySelector("#to-input");
const toInputDisplay = document.querySelector("#to-input-display");
let fromCurrency = "USD";
let toCurrency = "USD";
fromList.forEach((item) => {
item.addEventListener("click", () => {
fromBtn.innerText = item.innerText;
fromCurrency = item.innerText;
fromInputDisplay.innerText = currencyRatio[item.innerText]["unit"];
convert("from");
});
});
toList.forEach((item) => {
item.addEventListener("click", () => {
toBtn.innerText = item.innerText;
toCurrency = item.innerText;
toInputDisplay.innerText = currencyRatio[item.innerText]["unit"];
convert("from");
});
});
function convert(type) {
let amount = 0;
if (type == "from") {
amount = parseFloat(fromInput.value); // number
let convertedAmount = amount * currencyRatio[fromCurrency][toCurrency]; //number
toInput.value = convertedAmount.toLocaleString("en-US"); //string
fromInput.value = amount.toLocaleString("en-US"); //string
} else {
amount = parseFloat(toInput.value);
let convertedAmount = amount * currencyRatio[toCurrency][fromCurrency];
fromInput.value = convertedAmount;
}
}
fromInput.addEventListener("keyup", () => {
convert("from");
});
fromInput.addEventListener("click", () => {
fromInput.value = "";
});
toInput.addEventListener("keyup", () => {
convert("to");
});
toInput.addEventListener("click", () => {
toInput.value = "";
});
this is whole HTML code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Currency Exchange</title>
<link rel="stylesheet" href="style.css" class="rel" />
</head>
<body>
<div class="exchange-box">
<div class="dropdown">
<button class="dropbtn" id="from-button">USD</button>
<div class="dropdown-content" id="from-currency-list">
<a href="#">USD</a>
<a href="#">KRW</a>
<a href="#">GBP</a>
<a href="#">EUR</a>
</div>
</div>
<div class="input-area">
<input type="text" value="0.00" id="from-input" />
<div class="display" id="from-input-display">dollar</div>
</div>
</div>
<h1>=</h1>
<div class="exchange-box">
<div class="dropdown">
<button class="dropbtn" id="to-button">USD</button>
<div class="dropdown-content" id="to-currency-list">
<a href="#">USD</a>
<a href="#">KRW</a>
<a href="#">GBP</a>
<a href="#">EUR</a>
</div>
</div>
<div class="input-area">
<input type="text" value="0.00" id="to-input" />
<div class="display" id="to-input-display">dollar</div>
</div>
</div>
<script src="main2.js"></script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论