|
![]() |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
|||
![]()
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. |
#2
|
||||
|
||||
![]()
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 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)}); ![]() |
#3
|
|||
|
|||
![]()
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. |
#4
|
||||
|
||||
![]() Quote:
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, '')); 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); Code:
var ttnum = StringToNumber(Field("amnt").replace(/[^\d\.]/g, ''));
__________________
Dan Korn FusionPro Developer / JavaScript Guru / Forum Moderator PTI Marketing Technologies | Printable | MarcomCentral 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)}); ![]() |
#5
|
|||
|
|||
![]()
Thanks Dan! That's an awesome explanation. It helps people like me to understand better, so I can do better in the future.
|
![]() |
Thread Tools | Search this Thread |
Display Modes | |
|
|