EricC Posted March 23, 2010 Share Posted March 23, 2010 Hello gang, I have a Price Formatting Rule that does the following: - Removes the $ if the user entered it - Formats price with 2 digits after decimal point - Divides the dollars and cents - Puts it all back together as follows: superscript Dollar Sign + dollar amount + superscript cents amount works well, but I want to add two more 'features' 1. if the user enters text (the word 'FREE' for example) then I just want to bypass all of the rules above and return just the text. 2. if the user enters .00 then i just want to return the superscripted $ plus the dollar amount. (so ... $1 instead of $1.00) here is the rule I am using now... // set superscript font size and baseline shift in template: // Variabe Text Editor --> Paragraph... --> Global Settings... // Superscript (Ratio and Offset). // Set price field name here. priceVariable=Field("CouponPrice") // remove dollar sign if entered by user priceVariable=priceVariable.replace("$", ""); // format as price with two digits after decimal point // priceVariable=FormatNumber("##,###.00##", priceVariable); priceVariable=FormatNumber("##,###.00##", priceVariable); // divide up dollars and cents var dollarsCentsArray=priceVariable.split("."); dollars = dollarsCentsArray[0]; cents = dollarsCentsArray[1]; // format with superscripted dollar sign/cents if dollars are entered // or with superscripted cent sign if no dollars entered if (dollars!="") return "<superscript>$</superscript>"+dollars+"<superscript>"+cents+"</superscript>"; else return cents+"<superscript>¢</superscript>"; Link to comment Share on other sites More sharing options...
esmith Posted March 23, 2010 Share Posted March 23, 2010 Would the following modifications to your code work: var priceVariable = Field("CouponPrice"); var finalPrice = ""; // if CouponPrice contains text if (priceVariable.search(/[a-z]/i) > -1) { finalPrice = priceVariable; } // otherwise if CouponPrice does NOT contain text else { priceVariable = priceVariable.replace("$", ""); priceVariable = FormatNumber("##,###.00##", priceVariable); var dollarsCentsArray=priceVariable.split("."); var dollars = dollarsCentsArray[0]; var cents = dollarsCentsArray[1]; // if dollar value is present if (dollars != "") { // dollar amount only (no coins) if (cents == "00") { finalPrice = "<superscript>$</superscript>" + dollars; } // amount includes coins else { finalPrice = "<superscript>$</superscript>" + dollars + "<superscript>" + cents + "</superscript>"; } } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; } return finalPrice; Link to comment Share on other sites More sharing options...
EricC Posted March 23, 2010 Author Share Posted March 23, 2010 I love you. (that worked perfectly!) Link to comment Share on other sites More sharing options...
anthony.bice Posted June 17, 2010 Share Posted June 17, 2010 Wow, Thanks for this. It's so close to what I currently need. But; I've tried in vain for a day and half to modify/understand it and I haven't had success yet. I'm needing the rule to return the "just coins" (coins amount followed by cents symbol) formatting, when a user enters a leading zero in a price (0.25). Right now it's returning $0.25. I also would like it to strip out text and currency symbols. Makes me suspect that the "if CouponPrice contains text" statement at the beginning needs to change. Thanks alot. Link to comment Share on other sites More sharing options...
EricC Posted June 17, 2010 Author Share Posted June 17, 2010 don't have a way to test it right now, but this should work... REPLACE THE FOLLOWING BLOCK OF CODE: // if dollar value is present if (dollars != "") { // dollar amount only (no coins) if (cents == "00") { finalPrice = "<superscript>$</superscript>" + dollars; } // amount includes coins else { finalPrice = "<superscript>$</superscript>" + dollars + "<superscript>" + cents + "</superscript>"; } } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; ... with this ... // if dollar value is present if (dollars != "") { if (dollars == "0" || dollars == "00") { finalPrice = cents + "<superscript>¢</superscript>"; } else if (cents == "00") { finalPrice = "<superscript>$</superscript>" + dollars; } else { finalPrice = "<superscript>$</superscript>" + dollars + "<superscript>" + cents + "</superscript>"; } } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; Link to comment Share on other sites More sharing options...
anthony.bice Posted June 18, 2010 Share Posted June 18, 2010 That's working great, thanks Eric. I'm still trying to get it to strip out [a-z] as well. Can't successfully change or remove the "// if CouponPrice contains text" statement. Looks like this statement bypasses the rest of the rule if the entry contains text? Well at any rate I don't want to bypass anything...here's what I'm working with: var priceVariable = Field("Price (Enter in the following format 0.00)"); var finalPrice = ""; // if CouponPrice contains text if (priceVariable.search(/[a-z]/i) > -1) { finalPrice = priceVariable; } // otherwise if CouponPrice does NOT contain text else { priceVariable = priceVariable.replace("$", ""); priceVariable = FormatNumber("##,###.00##", priceVariable); var dollarsCentsArray=priceVariable.split("."); var dollars = dollarsCentsArray[0]; var cents = dollarsCentsArray[1]; // if dollar value is present if (dollars != "") { if (dollars == "0" || dollars == "00") { finalPrice = cents + "<superscript>¢</superscript>"; } else if (cents == "00") { finalPrice = "<superscript>$</superscript>" + dollars; } else { finalPrice = "<superscript>$</superscript>" + dollars + "." + cents; } } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; } return finalPrice; Link to comment Share on other sites More sharing options...
EricC Posted June 18, 2010 Author Share Posted June 18, 2010 you want to remove the text rule? how do you want to handle the output if the user DOES enter text? the first part of the rule uses a regex to search for any characters that are not numbers. if it finds anything, it just returns what the user entered. Link to comment Share on other sites More sharing options...
anthony.bice Posted June 18, 2010 Share Posted June 18, 2010 Yep. I need it to completely remove any text or additional dollar signs if entered by the user. I've got something to work here, I'm sure in quite an unconventional way...hmmm...Thanks again though Eric... var priceVariable = Field("Price (Enter in the following format 0.00)"); var finalPrice = ""; if (priceVariable =="") { finalPrice = priceVariable; } // if CouponPrice does contain text or dollar signs they will be omitted else { priceVariable = priceVariable.replace(/\$/gi, "").replace(/[a-z]/gi,""); priceVariable = FormatNumber("##,###.00##", priceVariable); var dollarsCentsArray=priceVariable.split("."); var dollars = dollarsCentsArray[0]; var cents = dollarsCentsArray[1]; // if dollar value is present if (dollars != "") { if (dollars == "0" || dollars == "00") { finalPrice = cents + "<superscript>¢</superscript>"; } else if (cents == "00") { finalPrice = "<superscript>$</superscript>" + dollars; } else { finalPrice = "<superscript>$</superscript>" + dollars + "." + cents; } } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; } return finalPrice;� Link to comment Share on other sites More sharing options...
teresas Posted July 1, 2010 Share Posted July 1, 2010 First of all, thank you so much for this; it was a great starting point. I added a bit more to allow the user to enter a quantity for amount ("2/$4") and adjusted it a bit to handle more formats entered by the user (I think I went through 20 different formats or so). Anyway, maybe someone else will find it useful. Thanks again! var priceVariable = Field("price"); var finalPrice = ""; // if price contains text if (priceVariable.search(/[a-z]/i) > -1) { finalPrice = priceVariable; } // if price contains / else if (priceVariable.search("/") > -1) { var perAmountArray=priceVariable.split("/"); var per = perAmountArray[0]; var amount = perAmountArray[1]; amount = amount.replace("$", ""); amount = FormatNumber("##,###.00##", amount); var dollarsCentsArray=amount.split("."); var dollars = dollarsCentsArray[0]; var cents = dollarsCentsArray[1]; var howMuch = ""; // if dollar value is present if (dollars > "0") { // dollar amount only (no coins) if (cents == "00") { howMuch = "<superscript>$</superscript>" + dollars; } // amount includes coins else { howMuch = "<superscript>$</superscript>" + dollars + "<superscript>" + cents + "</superscript>"; } } // less than $1 (just coins) else { howMuch = cents + "<superscript>¢</superscript>"; } finalPrice = "<superscript>" + per + "/</superscript>" + howMuch; } // otherwise if price does NOT contain text or / else { priceVariable = priceVariable.replace("$", ""); priceVariable = FormatNumber("##,###.00##", priceVariable); var dollarsCentsArray=priceVariable.split("."); var dollars = dollarsCentsArray[0]; var cents = dollarsCentsArray[1]; // if dollar value is present if (dollars > "0") { // amount includes coins finalPrice = "<superscript>$</superscript>" + dollars + "<superscript>" + cents + "</superscript>"; } // less than $1 (just coins) else finalPrice = cents + "<superscript>¢</superscript>"; } return finalPrice; Link to comment Share on other sites More sharing options...
rbasel Posted August 9, 2011 Share Posted August 9, 2011 This is brilliant. If I had to use this in another currency, would all the references to dollar/s have to change. IE: My currency is Rands ®? Do any of you javascripters out there sell your services freelance? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.