价格格式,小数点后更改大小

huangapple go评论64阅读模式
英文:

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($(&#39;.price-lbl-cust&#39;), function() {
    var price = $(this).html();
    $(this).html(price.replace(/(\D*)(\d*\,)(\d*)/,
        &#39;&lt;span style=&quot;font-size:16px;font-weight:600;&quot;&gt;$1&lt;/span&gt;&lt;span style=&quot;font-size: 1.675rem;line-height: 1.5rem;font-weight:600;&quot;&gt;$2&lt;/span&gt;&lt;span style=&quot;font-size: .92222em;font-weight:600;&quot;&gt;$3&lt;/span&gt;&#39;
    ));
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;price-lbl-cust&quot;&gt;144,40&lt;/div&gt;

<!-- 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($(&#39;.price-lbl-cust&#39;), function() {
    var price = $(this).html();
    $(this).html(price.replace(/(\d*\,)(\d*\,)*(\d+)/,
        &#39;&lt;span style=&quot;font-size: 1.675rem;line-height: 1.5rem;font-weight:600;&quot;&gt;$1&lt;/span&gt;&lt;span style=&quot;font-size: 1.675rem;line-height: 1.5rem;font-weight:600;&quot;&gt;$2&lt;/span&gt;&lt;span style=&quot;font-size: .92222em;font-weight:600;&quot;&gt;$3&lt;/span&gt;&#39;
    ));
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;price-lbl-cust&quot;&gt;2,948,11&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 18:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定