PDA

View Full Version : Tweak to JavaScript Global Rule


spyderwebceo21
September 16th, 2011, 08:29 AM
We had pti write a global rule for one of our templates and we need it tweaked to include when our client has a price like this 3.49-4.49



function myPrice(price)

{

var trimPrice = ReplaceSubstring(ReplaceSubstring(price,'$',''),', ','');
var decimalPrice = FormatNumber("0.00", trimPrice);
var SuperPrice = Right(decimalPrice, 3);
var NormalPrice = Left(decimalPrice, decimalPrice.length-3);
var CentPrice = Right(decimalPrice, 2);


var patt=/\d+/;
var result = patt.test(price);

if (result == false)
{
var newPrice = price;
}

else if (result == true)
{

if (price.indexOf("/") > -1 || price.indexOf("%") > -1)
{
var newPrice = ReplaceSubstring(price, "$", "<superscript>$</superscript>");
}
else
{

if (StringToNumber(decimalPrice) >= 1)
{
if (price != "" && price.indexOf("$") < 0)
{
var newPrice = NormalPrice+"<superscript>"+SuperPrice+"</superscript>";
}

else if (price != "" && price.indexOf("$") > -1)
{
var newPrice = "<superscript>$</superscript>"+NormalPrice+"<superscript>"+SuperPrice+"</superscript>";
}

else if (price == "")
{
var newPrice = "";
}
}
else if (StringToNumber(decimalPrice) < 1)
{
if (price != "")
{
var newPrice = CentPrice+"<superscript>&cent;</superscript>";
}

else if (price == "")
{
var newPrice = "";
}
}
}
}
return newPrice;
}

Dan Korn
September 20th, 2011, 09:32 AM
Add this function after the other global one and call it instead:
function myPrices(prices)
{
return String(prices).replace(/(\d+\.*\d+)/g, function(p){return myPrice(p)});
}