Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 23rd, 2010, 09:40 AM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Arrow Complex Price Format Rule

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...


Code:
// 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>";
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #2  
Old March 23rd, 2010, 12:42 PM
esmith's Avatar
esmith esmith is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Charlotte, NC
Posts: 870
Default Re: Complex Price Format Rule

Would the following modifications to your code work:
Code:
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;
__________________
Eric Smith
Content Developer
Classic
FP 9.3.6, MAC OSX 10.9.4, Acrobat 11.0.09
Reply With Quote
  #3  
Old March 23rd, 2010, 12:54 PM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Default Re: Complex Price Format Rule

I love you.




(that worked perfectly!)
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #4  
Old June 17th, 2010, 02:54 PM
anthony.bice anthony.bice is offline
Junior Community Member
 
Join Date: Jun 2009
Location: Houston Texas
Posts: 23
Default Re: Complex Price Format Rule

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.
__________________
Anthony Bice
Pointsmith LP - Houston, TX
FPVDPD 9.3.36, MAC OSX 10.11.3, Acrobat 11.0.10
Reply With Quote
  #5  
Old June 17th, 2010, 03:46 PM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Default Re: Complex Price Format Rule

don't have a way to test it right now, but this should work...

REPLACE THE FOLLOWING BLOCK OF CODE:

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 ...


Code:
// 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>";
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #6  
Old June 18th, 2010, 11:05 AM
anthony.bice anthony.bice is offline
Junior Community Member
 
Join Date: Jun 2009
Location: Houston Texas
Posts: 23
Default Re: Complex Price Format Rule

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:
Code:
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;
__________________
Anthony Bice
Pointsmith LP - Houston, TX
FPVDPD 9.3.36, MAC OSX 10.11.3, Acrobat 11.0.10
Reply With Quote
  #7  
Old June 18th, 2010, 02:17 PM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Default Re: Complex Price Format Rule

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.
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #8  
Old June 18th, 2010, 02:58 PM
anthony.bice anthony.bice is offline
Junior Community Member
 
Join Date: Jun 2009
Location: Houston Texas
Posts: 23
Default Re: Complex Price Format Rule

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...

Code:
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;�
__________________
Anthony Bice
Pointsmith LP - Houston, TX
FPVDPD 9.3.36, MAC OSX 10.11.3, Acrobat 11.0.10

Last edited by anthony.bice; June 21st, 2010 at 07:15 AM..
Reply With Quote
  #9  
Old July 1st, 2010, 11:01 AM
teresas teresas is offline
Junior Community Member
 
Join Date: Jun 2010
Location: Chicago
Posts: 17
Default Re: Complex Price Format Rule

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!

Code:
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;
Reply With Quote
  #10  
Old August 8th, 2011, 09:09 PM
rbasel rbasel is offline
Forum n00b
 
Join Date: Aug 2011
Posts: 1
Default Re: Complex Price Format Rule

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 (R)?

Do any of you javascripters out there sell your services freelance?
Reply With Quote
Reply

Tags
format, price

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:27 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.