Price Percentage Calculator
การเขียนคำสั่ง Javacript คำนวณราคาส่วนลดขาย ให้อยู่ในรูปแบบของเปอร์เซ็นต์(Percent)ส่วนลด โดยคำนวณจากราคาต้นทุน(Price) และให้แปรผันตามเปอร์เซ็นต์ส่วนลด ซึ่งในที่นี้จะได้ราคาส่วนลดมาจาก 2 ตัวแปร คือ ราคาและเปอร์เซ็นต์
สูตรการคำนวณ มาจากการกำหนด (ราคา-(ราคาxเปอร์เซ็นต์)/100) ซึ่งจะได้ผลลัพท์ออกมาอยู่ในรูปส่วนลดที่เป้นเงินบาท ทั้งนี้สามารถนำไปประยุกต์ใช้ในงานส่วนลดสินค้าต่างๆ ได้ตามความเหมาะสม
Price Percentage Calculator Javascript
0 ฿
0 ฿
➤ HTML Code<p>
<label>Price:</label>
<input id="inputPrice" type="" placeholder="price" value="1000">
</p>
<p>
<label>Percent:</label>
<input id="inputPercent" type="" placeholder="%" oninput="priceConverter(this.value)" onchange="priceConverter(this.value)">
</p>
<p>
<label>Discount:</label>
<span id="discountPrice">0 ฿</span>
</p>
<p>
<label>Total Price:</label>
<span id="salePrice">0 ฿</span>
</p>
➤ Javascript Code<script>
function priceConverter(inputPercent) {
var inputPrice = document.getElementById("inputPrice").value;
var discountPrice = ((inputPercent*inputPrice)/100);
var salePrice = (inputPrice-(inputPercent*inputPrice)/100);
document.getElementById("discountPrice").innerHTML= discountPrice.toLocaleString()+' ฿';
document.getElementById("salePrice").innerHTML= salePrice.toLocaleString()+' ฿';
}
</script>
ความคิดเห็น
แสดงความคิดเห็น