Skip navigation
Quelluomo
Currently Being Moderated

Changing number of decimals depending on field value

Apr 5, 2012 8:47 AM

Hi everybody,

I'm trying to make a form using Adobe Acrobat.

I want a field that usually has only 2 decimal positions, to have 4 decimal positions ONLY when needed.

Example: if field value is 3,53 it's fine, but if value is 3,5346 I want it to show it all.

I've played around field properties and I've realized that I need to use a custom format script. I don't know where to start, though. Can you point me in the right direction?

Thank you in advance.

 
Replies
  • George Johnson
    11,731 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 5, 2012 9:03 AM   in reply to Quelluomo

    Exactly how is the field value being set? What if the value is 3.5300?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 5, 2012 3:19 PM   in reply to Quelluomo

    Is this a calculated field? If so, then simply don't use any formatting

    settings at all, and it will automatically adjust the number of decimals

    shown to the most significant number.

     
    |
    Mark as:
  • George Johnson
    11,731 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 5, 2012 4:41 PM   in reply to Quelluomo

    I can suggest a script, but it would help if you described exactly how you set up the current format. For example, what do you want to use for the currency character (if any), the thousands separator, and the decimal separator.

     
    |
    Mark as:
  • George Johnson
    11,731 posts
    Aug 11, 2002
    Currently Being Moderated
    Apr 5, 2012 5:13 PM   in reply to Quelluomo

    I just tossed the following together quickly and didn't do much testing, to take is as general guidance. It is intended to be placed in a document-level JavaScript and called from the field's custom Format script. You can't simply place it all as the Format script due to the way Acrobat behaves when you do.

     

    The point of the script is to calculate the number to use as the first parameter to the AFNumberFormant function, which is what Acrobat uses when you set up a numeric format. I couldn't think of a clever name for the function, so I'll leave that to you:

     

    function fmt1() {
    
        // Set the minimum number of digits to the right of the decimal
        var min = 2;
    
        // Get the field value, as a string
        var val = event.value;
    
        // Round to the nearest 10-thousand of a cent
        var rn = util.printf("%.4f", val);
    
        // Replace any trailing zeroes with nothing
        rn = rn.replace(/[0]+$/, "");
    
        // Get the number of characters after the decimal
        var num = rn.split(".")[1].length;
    
        // Set to the minimum if appropriate
        if (num < 2) num = 2;
    
        // Use built-in formatting function
        AFNumber_Format(num, 2, 0, 0, "\u20ac", true);
    
    }
    

     

    Call it like this in the field's Format event:

     

    fmt1();
    

     

     

    If this is not a calculated field, but one that a user will interact with, you'll want to add a corresponding Keystroke function, something like:

     

    function keystroke1() {
    
        AFNumber_Keystroke(2, 2, 0, 0, "\u20ac", true);
    
    }
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points