Skip to main content
vigneshs53970460
Known Participant
January 23, 2018
Answered

change negative numbers to parentheses in an acrobat form

  • January 23, 2018
  • 14 replies
  • 4622 views

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.

This topic has been closed for replies.
Correct answer Thom Parker

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.

14 replies

vigneshs53970460
Known Participant
January 29, 2018

How do I get professional help??

Thom Parker
Community Expert
Community Expert
January 29, 2018

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

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
vigneshs53970460
Known Participant
January 25, 2018

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

Thom Parker
Community Expert
Community Expert
January 25, 2018

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
vigneshs53970460
Known Participant
January 25, 2018

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

vigneshs53970460
Known Participant
January 25, 2018

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.

Thom Parker
Community Expert
Community Expert
January 25, 2018

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
vigneshs53970460
Known Participant
January 25, 2018

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

vigneshs53970460
Known Participant
January 25, 2018

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.

vigneshs53970460
Known Participant
January 25, 2018

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

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

vigneshs53970460
Known Participant
January 24, 2018

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

Thom Parker
Community Expert
Community Expert
January 24, 2018

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
vigneshs53970460
Known Participant
January 23, 2018

Can anyone help me please??

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
January 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 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
vigneshs53970460
Known Participant
January 23, 2018

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