Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > FusionPro® VDP Creator

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old December 15th, 2021, 07:31 AM
Rick J. Rick J. is offline
Junior Community Member
 
Join Date: Sep 2012
Posts: 19
Default Multiplied number(text) not returning the correct amount

I'm working on trying to multiply a dollar amount times two. Sounds simple. The problem is the file I receive daily is a tab delimited text file where all fields have been formatted as text. My rule "2xAmt" works on amounts under $1,000.00, but I believe because of the comma any amount equal to or over $1,000.00 does not return the proper amount. I'm using the "2xAmt "rule in the "FinishingForward" rule where I'm bringing in data through javascript so the text flows properly.

There is no way to get the file formatted different than it is now. I'm stuck with what I recieve for a data file to work with.

Is there any way I can make this happen? Im attaching the files for review. Any help would be appreciated.

Thanks in advance,
Rick J.
Attached Files
File Type: zip FP_AppealLtrTmplt.zip (10.26 MB, 5 views)
Reply With Quote
  #2  
Old December 15th, 2021, 02:38 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,623
Default Re: Multiplied number(text) not returning the correct amount

Yes, it's always better to deal with raw, unformatted data, such as pure numbers, rather than pre-formatted data, such as with dollar signs and comma separators. But I understand that you just have what you have.

So you're stripping off the dollar sign to get a number, but not the commas that divide thousands (and millions). There are lots of ways to "clean" or "unformat" such strings in JavaScript, which can be found via a search like this, but I think this is the simplest:
Code:
var ttnum = StringToNumber(Field("amnt").replace(/[^\d\.]/g, ''));
return FormatNumber("$#,###.00", ttnum * 2);
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #3  
Old December 16th, 2021, 07:28 AM
Rick J. Rick J. is offline
Junior Community Member
 
Join Date: Sep 2012
Posts: 19
Default Re: Multiplied number(text) not returning the correct amount

Thank you Dan for the quick reply. I knew there had to be a way to get rid of the commas and decimal point, but had no idea how to go about that. Is there anyway you could just explain what (/[^\d\.]/g, '') that all means. I know it's to strip all those out, but I'm very curious as to what the symbols mean. I don't see anything that tells it to remove the commas and decimals.

Thanks again. Worked perfect.
Reply With Quote
  #4  
Old December 16th, 2021, 12:12 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,623
Default Re: Multiplied number(text) not returning the correct amount

Quote:
Originally Posted by Rick J. View Post
Is there anyway you could just explain what (/[^\d\.]/g, '') that all means. I know it's to strip all those out, but I'm very curious as to what the symbols mean. I don't see anything that tells it to remove the commas and decimals.
Yes, sorry, this deserves some explanation.

The JavaScript String.replace() function uses a shorthand called Regular Expressions.

Regular Expressions are a big topic, so I won't try to fully explain them here, but I can break down the call here:
Code:
var ttnum = StringToNumber(Field("amnt").replace(/[^\d\.]/g, ''));
So we're calling the String.replace function, on the String object returned by the Field function for the field "amnt". This gives us a string with a value such as "$1,234.56".

The String.replace() function takes two parameters, which, in this case, are a RegExp literal and a replacement string (which is '', or an empty string).

The RegExp literal /[^\d\.]/g breaks down like so:

/ - starts the RegExp literal
[ - starts a range/group (see Groups and Ranges)
^ - denotes that the range is a "negated" group
\d - denotes "any digit" (0-9) (see Character Classes)
\. - denotes a literal period/dot (decimal place) character
] - ends the range/group
/ - ends the RegExp literal
g - "global" flag, denoting to act on all matching instances

So [^\d\.] means "a group of characters including any digit and a period", negated with the caret ^, turning it into "any character that's NOT a digit or period." And the g flag says "ALL matches (instances) of any character that's not a digit or period."

So what this means is "match all instances of a character that is NOT a digit or a period/dot," and replace it with the replacement, which is '', i.e. an empty string, so it's replacing it with nothing, i.e. removing it.

Effectively, we're stripping out anything that's not a digit or a dot, including the dollar sign, any commas, and anything else, leaving just the digits and the decimal dot.

Therefore, a string such as "$1,234.56" gets turned into "1234.56", which can be parsed to the number 1234.56, which can then be used in mathematical calculations, such as multiplying it by two.

Now, sure, instead of using Regular Expressions, we could have written something like this:
Code:
var ttstr = Field("amnt");
var ttnumstr = "";
for (var i in ttstr)
{
    var c = ttstr[i];
    if ("1234567890.".indexOf(c) >= 0)
        ttnumstr += c;
}
var ttnum = StringToNumber(ttnumstr);
But this is much more succinct:
Code:
var ttnum = StringToNumber(Field("amnt").replace(/[^\d\.]/g, ''));
And, once you get the hang of Regular Expressions, you'll find them to be an extremely powerful tool.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #5  
Old December 16th, 2021, 12:18 PM
Rick J. Rick J. is offline
Junior Community Member
 
Join Date: Sep 2012
Posts: 19
Default Re: Multiplied number(text) not returning the correct amount

Thanks Dan! That's an awesome explanation. It helps people like me to understand better, so I can do better in the future.
Reply With Quote
Reply

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 10:43 PM.


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