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
  • 4698 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 23, 2018

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

try67
Community Expert
Community Expert
January 23, 2018

Post your full code, please.

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

vigneshs53970460
Known Participant
January 23, 2018

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.

vigneshs53970460
Known Participant
January 23, 2018

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

vigneshs53970460
Known Participant
January 23, 2018

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

try67
Community Expert
Community Expert
January 23, 2018

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