英文:
Price format, change size after decimal
问题
我使用以下代码来格式化价格:
$.each($('.price-lbl-cust'), function() {
var price = $(this).html();
$(this).html(price.replace(/(\D*)(\d*\,)(\d*)/,
'<span style="font-size:16px;font-weight:600;">$1</span><span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$2</span><span style="font-size: .92222em;font-weight:600;">$3</span>'
));
});
这会给我以下输出:
是正确的。
但问题是当价格例如为:
2,948,11
然后给我以下输出:
只有第一个数字2是大的,第二个948和第三个11是小的。
如何更改和添加,当价格有2个小数点时,使最后一个小数点后的价格变小?
英文:
I use this code to format price:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$.each($('.price-lbl-cust'), function() {
var price = $(this).html();
$(this).html(price.replace(/(\D*)(\d*\,)(\d*)/,
'<span style="font-size:16px;font-weight:600;">$1</span><span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$2</span><span style="font-size: .92222em;font-weight:600;">$3</span>'
));
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="price-lbl-cust">144,40</div>
<!-- end snippet -->
this give me output:
is correct.
But the issue is when price is example:
2,948,11
then give me output:
only first number 2 is big, and second 948 and third 11 is small.
How to change and add when price have 2 decimal then make small price only after last decimal ?
答案1
得分: 0
/(\d*\,)(\d*\,)*(\d+)/
确保选择最后一个逗号之前的所有内容。
$.each($('.price-lbl-cust'), function() {
var price = $(this).html();
$(this).html(price.replace(/(\d*\,)(\d*\,)*(\d+)/,
'<span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$1</span><span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$2</span><span style="font-size: .92222em;font-weight:600;">$3</span>'
));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="price-lbl-cust">2,948,11</div>
英文:
Try this :
/(\d*\,)(\d*\,)*(\d+)/
will make sure to select everything before the last comma.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$.each($('.price-lbl-cust'), function() {
var price = $(this).html();
$(this).html(price.replace(/(\d*\,)(\d*\,)*(\d+)/,
'<span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$1</span><span style="font-size: 1.675rem;line-height: 1.5rem;font-weight:600;">$2</span><span style="font-size: .92222em;font-weight:600;">$3</span>'
));
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="price-lbl-cust">2,948,11</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论