gpt4 book ai didi

jquery - 如何从价格(货币)中删除逗号或点后的零

转载 作者:太空宇宙 更新时间:2023-11-03 19:35:39 28 4
gpt4 key购买 nike

我正在尝试删除 之后的尾随零。 以一种货币表示,仍然保留货币符号。例如,€90.00€9090.00€90€€90.33€90.33

示例如下:

$('.product-price-wrapp').each(function() { 
var productPrice = $(this).find('.price').text();
var trimPrice = productPrice.substr(0,productPrice.indexOf(','));
productPrice.text(trimPrice);
});
body {
padding: 20px;
background: #20262E;
font-family: Helvetica;
}

.price {
color: green;
}

.price span {
color: #747474;
}

.price:first-child,
.price:last-child,
.price:nth-of-type(6){
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>

<div class="product-price-wrapp">
<div class="price"><span>Product Price</span> 90,00 €</div>
<br>
<div class="price"><span>Product Price</span> 90,30 €</div>
<br>
<div class="price"><span>Product Price</span> 90,45 €</div>
<br>
<div class="price"><span>Product Price</span> 92,67 €</div>
<br>
<div class="price"><span>Product Price</span> 33.33 €</div>
<br>
<div class="price"><span>Product Price</span> £22.00</div>
<br>
<div class="price"><span>Product Price</span> £72,22</div>
<br>
<div class="price"><span>Product Price</span> £72,00</div>
</div>

工作版本:JSFiddle

你们都很棒,非常感谢你们的帮助!信用:@Cocest @A。拉莫斯@Ivan

最佳答案

您可以使用正则表达式来检测价格是否包含尾随逗号:,00.00,如果是,您可以将其替换为空字符串。

我用过这个正则表达式:

/[\.,]0{2}/
  • [\.,] 搜索 .,

  • 0{2} 后跟两个零

这是完整的代码。

$('.product-price-wrapp > .price').each(function(index, element) {

var priceElement = $(element);

priceElement.text(
'Product Price ' + priceElement.text().substring(14).replace(/[\.,]0{2}/, '')
);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>

<div class="product-price-wrapp">
<div class="price"><span>Product Price</span> 90,00 €</div>
<br>
<div class="price"><span>Product Price</span> 90,30 €</div>
<br>
<div class="price"><span>Product Price</span> 90,45 €</div>
<br>
<div class="price"><span>Product Price</span> 92,67 €</div>
<br>
<div class="price"><span>Product Price</span> 33.33 €</div>
<br>
<div class="price"><span>Product Price</span> £22.00</div>
<br>
<div class="price"><span>Product Price</span> £72,22</div>
<br>
<div class="price"><span>Product Price</span> £72,00</div>
</div>

关于jquery - 如何从价格(货币)中删除逗号或点后的零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54160935/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com