Remove zeros after a comma from price, but leave currency symbol.
https://stackoverflow.com/questions/54160935/how-to-remove-zeros-after-a-comma-or-dot-from-price-currency/54162238#54162238
by JN-FRONTEND
HTML
<div class="product_price_wrapp">
<div class="price">
<span>Product I Price</span>
<span class="price_tag">90,00 €</span>
</div>
<br>
<div class="price">
<span>Product II Price</span>
<span class="price_tag">90,30 €</span>
</div>
<br>
<div class="price">
<span>Product III Price</span>
<span class="price_tag">90,45 €</span>
</div>
<br>
<div class="price">
<span>Product IV Price</span>
<span class="price_tag">92,67 €</span>
</div>
<br>
<div class="price">
<span>Product V Price</span>
<span class="price_tag">33.33 €</span>
</div>
<br>
<div class="price">
<span>Product VI Price</span>
<span class="price_tag">£22.00</span>
</div>
<br>
<div class="price">
<span>Product VII Price</span>
<span class="price_tag">£72,22</span>
</div>
<br>
<div class="price">
<span>Product VIII Price</span>
<span class="price_tag">£72,00</span>
</div>
</div>
CSS
body {
padding: 20px;
background: #20262E;
font-family: Helvetica;
}
.price span:first-child {
color: #747474;
}
.price .price_tag{
color: green;
}
.price:first-child .price_tag,
.price:last-child .price_tag,
.price:nth-of-type(6) .price_tag{
color: red;
}
JavaScript
// OPTION 1
$('.product_price_wrapp .price .price_tag').each(function(){
var price = $(this).text();
price = price.replace(/(\d+)([.,]0+)/, "$1");
$(this).text(price);
});
/*
**
REGULAR EXPRESSION: /[\.,]0{2}/
[\.,] : Searches for either . or ,
0{2} : Followed by two zeros
$1 = (\d+) : gets all one number or more.
$2 = ([.,]0+) : gets all the trailing zero with dot or comma on its first character.
**
*/
// OPTION 2
/*
$('.product_price_wrapp .price .price_tag').each(function(){
var currency = $(this).text();
if (/[.]/.test(currency)) {
if (/\d+[.]0+$/.test(currency)) {
currency = currency.split('.')[0];
$(this).text(currency);
} else if (/^\d+[.]0+/.test(currency)) {
var sign = currency.split(/.0+/);
currency = currency.split('.')[0] + sign[sign.length - 1];
$(this).text(currency);
}
} else if (/[,]/.test(currency)) {
if (/\d+[,]0+$/.test(currency)) {
currency = currency.split(',')[0];
$(this).text(currency);
} else if (/^\d+[,]0+/.test(currency)) {
var sign = currency.split(/,0+/);
currency = currency.split(',')[0] + sign[sign.length - 1];
$(this).text(currency);
}
}
});
*/
// OPTION 3 ( Removing everything after . or , )
/*
$('.product_price_wrapp .price .price_tag').each(function(){
var price = $(this).text();
if (price.indexOf(",") >= 0 || price.indexOf(".") < 0)
{
var contentPrice = price.substr(0, price.indexOf(','));
$(this).text(contentPrice + ' option A');
}
if (price.indexOf(".") >= 0 && price.indexOf(",") < 0) {
var intvalue = price.substr(0, price.indexOf('.'));
$(this).text(intvalue + ' option B');
}
// Testing
if (price.indexOf(",") >= 0 && price.indexOf(".") < 0) {
var intvalue = (parseInt(price.replace(/[^0-9\.]/g, '')));
//$(this).text(intvalue + ' option C');
}
});
*/