• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

change negative numbers to parentheses in an acrobat form

Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Hi,

I'm having one text field (Text1)  and one number field (NumberField) in an acrobat form and I need to pull the data from number field to the text field.

Sometimes the NumberField value is in negative numbers. So, I need to change the negative numbers to parentheses ().

var num = this.getField("NumberField").value;

var str = num.toString();

var strlen = str.lenght;

var value1 = util.printf("%, 0 0.2f", num);

if (num == "") {

event.value = "This part contains: "

} else {

even.value = "This part contains: + value1;

}

Above is the script to get the value from number field and formatting decimal as well.

Please help me to change the negative number to parentheses.

TOPICS
Acrobat SDK and JavaScript

Views

2.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 23, 2018 Jan 23, 2018

I think you are making this way too complicated. The first thing you need to do is state exactly what it is you are starting with and what you want to see in the "TextField". If all you want to do is use a number formatted with parentheses for negative. Then this code will do the conversion.

var originalNum = this.getFeild("NumField").value;

var strNumber = (originalNum<0)?"("+Math.abs(originalNum) + ")":String(originalNum);

If you want more specific formatting then add the util.printf() function t

...

Votes

Translate

Translate
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Why not simply use the built-in Number format options that do that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

var num = this.getField("Text1").value;

var string = num;

var results = string.replace(/\B(?=(\d{3})+\b)/g, ",").replace(/-(.*)/, "($1)");

console.println(results);

I tried this above code but it's not working showing some error message like below:

TypeError: string.replace is not a function

3: Console:Exec

undefined

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

No I need to add the following text as well "This part contains: " to the text field

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Try using the valueAsString property instead of value.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Picture2.png

Thank you, valueAsString is working but I don't know, how to loop this with my first script.

Please help me to get this thing solve.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

I'm getting "string is undefined" error. When I loop the code in my original code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Post your full code, please.

On 23 January 2018 at 11:28, vigneshs53970460 <forums_noreply@adobe.com>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

JS is case-sensitive. It's valueAsString, not valueAsstring...

In the future please post your code as text, not as an image.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

var num = this.getField("Text1").value;

var trim = this.getField("Text2").value;

var str = num.toString();

var strlen = str.length;

var string1 = trim.toString();

var result = string.replace(/\B(?=(\d{3})+\b)/g, ",").replace(/-(.*)/, "($1)");

var trimresult = string1.replace(/\s+/g, ' ');

if (num < 0) {

event.value = "This part contains: + trimresult + result"

}

In this code where to add decimal to get the value like (00000.00).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Can anyone help me please??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

You need to be aware of how JavaScript handles numbers. Unless the number is very large or very small JavaScript assumes the numeric value is to be treated as being in the IEEE floating point format. Or if one uses the "typeof" operator the result will be "number" not "string". You want to force the number to be be treated like a string then you need to force JavaScript to make it a string by using the "String" constrictor or if obtaining the value of a field use the "valueAsString" method. One needs to be careful not to let JavaScript convert the string if numeric values bake to an IEE floating point number.

I would obtain the value of the "Text1" field using the "valueAsString" property. This should also keep any leading zeros if they were entered into the field. Then when ever I used the variable for the value of the field i would use the "String" constrictor to force JavaScript to keep the string format. There is also a "Number" constrictor to force a string of numeric characters to an IEE floating point value or other numeric value.

If needed you can use the "substr" method to get the right most characters of a string that has leading zeros concatenated to the front of the string.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

I think you are making this way too complicated. The first thing you need to do is state exactly what it is you are starting with and what you want to see in the "TextField". If all you want to do is use a number formatted with parentheses for negative. Then this code will do the conversion.

var originalNum = this.getFeild("NumField").value;

var strNumber = (originalNum<0)?"("+Math.abs(originalNum) + ")":String(originalNum);

If you want more specific formatting then add the util.printf() function to the code.

If you want to preserve user entered leading zeros, then it gets more complicated with handling the negative numbers.

But the point is that the you need a very clear description of what it is you are trying to accomplish in order for us to help you.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Many thanks for your code but one thing whatever the value in ("NumField") including the decimals which is to be updated on ("Text1") field.

Below code which I used to get the value from (NumField) to (Text1)

var num = this.getField("NumField").value;

var dec = parseFloat(num).toFixed(2);

event.value = "This part contains: " + " " + dec;

In the above code I wrote "parseFloat(num)to.Fixed(2)" so the decimal value will be ".00" even though the "NumField" is having 3 or 4 decimals it will not take. So, I need to fix it based on the value which is in the "NumField" including the decimals and get updated on "Text1".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

That code works fine. There are a few ways you could do the same thing, but there is no reason to change if it works. Just add this line after the "parseFloat" bit to add the () for a negative before setting the output.

dec = (dec<0)?"("+Math.abs(dec) + ")":String(dec);

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Thanks for the script, Can we add thousand in this script?

The output should be "1,000.0523" instead of "1000.0523"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

var num  = this.getField("NumField").value;

var currType = this.getField("CurrencyType").value;

var dec  = parseFloat(num).toString();

var string = currType.toString();

var trim = string.replace(/\s+g, '');

var result = dec.replace(/\B(?=(\d{3})+\b)/g, ",").replace(/-(.*)/, "($1)");

if (num == "") {

event.value = "This part contains: ";

} else if (num < 0) {

event.value = "This part contains: " + trim + " " + result;

} else {

event.value = "This part contains: " + trim + " " + result;

The output should be "This part contains: EUR 1,000.0531"

But now I'm getting "This part contains: EUR 1,000.0,531"

I almost got the output only the thousand separator needs to remove from the decimals and I tried both the ways but it's not working.

Help me to get this work done.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

And decimal values will be dynamic it will keep on changing. So, we should not fix any particular decimal values.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Now I am facing another issue, when we add "1,000.00" value in "NumField" the value of ("Text3") is getting "1,000" my output should be what the value in "NumField" should exactly get the value for "Text3". if the "NumField" is "20,000.00" output should be "20,000.00" or if the value is "1,000.444" it should be the same.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

If you want it to be exact, then you need to use the exact text that was entered. I'm assuming that you still want to replace the minus symbol with parentheses?

var num  = this.getField("NumField").value;

var strNum = this.getField("NumField").valueAsString.replace(/^\-/,"");

if(num<0)  strNum = "("+strNum+")";

That's it. Use strNum to build your string. Very simple

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2018 Jan 25, 2018

Copy link to clipboard

Copied

Many thanks for you.

if the value ("NumField") does not have a decimal, what should we do??

when I tried to enter "NumField" value as "1" the output is 1.undefined and it should take either it is decimal or non-decimal and the output should be "This part contains: EUR 1" or "This part contains: EUR 1.0".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2018 Jan 25, 2018

Copy link to clipboard

Copied

Screenshot.png

I think this image will show you how exactly I want the output.

Kindly send me the code. it would be more helpful for me

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2018 Jan 25, 2018

Copy link to clipboard

Copied

You're question was about replacing a minus symbol with parentheses and you said you wanted the number verbatim. If you have a more complicated set of rules then those rules have to clearly stated up front. I think it is time for you to see professional help.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

How do I get professional help??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

LATEST

First you need a budget, then you send me a message

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines