[英]How can I format number shown in HTML to currency format, including the $ symbol and placing decimals with <sup></sup>?
I have a page with several HTML text strings with numbers, let's say one is 123.20. 我的页面上有几个带有数字的HTML文本字符串,比方说一个是123.20。 All of them are enclosed at third level in a div with the "money" class. 所有这些都包含在“货币”类的div的第三级中。 I want to change all of them to the format $123 .20 (the number is different for each of them) using JS. 我想使用JS将它们全部更改为$ 123 .20格式(每个数字不同)。
I cannot include the "money" class to the any other div than the first shown below. 除了下面显示的第一个之外,我无法将“ money”类包含到其他任何div中。 I cannot format this into the backend (even if I would love to). 我无法将其格式化为后端(即使我愿意)。
I don't know where to start from. 我不知道从哪里开始。
This is an example of the current HTML. 这是当前HTML的示例。
<div class="elementor-element elementor-element-11138f85 elementor-widget__width-auto money elementor-widget elementor-widget-text-editor" data-id="11138f85" data-element_type="widget" data-widget_type="text-editor.default">
<div class="elementor-widget-container">
<div class="elementor-text-editor elementor-clearfix">123.20</div>
</div>
</div>
This is what I would need to get displayed: 这是我需要显示的内容:
<div class="elementor-element elementor-element-11138f85 elementor-widget__width-auto money elementor-widget elementor-widget-text-editor" data-id="11138f85" data-element_type="widget" data-widget_type="text-editor.default">
<div class="elementor-widget-container">
<div class="elementor-text-editor elementor-clearfix">$123<sup>.20</sup></div>
</div>
</div>
Here is a sample using split()
这是使用split()
的示例
// find all the elements with your `money` class
var elements = document.getElementsByClassName('money')
// loop through all the `money` elements
for (var i = 0; i < elements.length; i++) {
// split the money value into two parts (before the decimal and after)
var amounts = elements[i].innerHTML.split('.')
// update the content to your formatted version
elements[i].innerHTML = amounts[0] + '<sup>.' + amounts[1] + '</sup>'
}
Intl.NumberFormat - MDN 国际号码格式-MDN
var number = 123456.789;
console.log(new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(number));
Output 输出量
₺123.456,79
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.