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

How to set a number to only show three decimal places with no rounding

New Here ,
Mar 08, 2022 Mar 08, 2022

Hi! I am trying to create a field that only allows the following criteria:

 

1. A number from 0-100

2. Only allow for three decimal places

3. The third decimal place doesn't round if more than three decimal places are entered

 

I have tried using the Number formatting and setting the decimal place to three while setting a limit of six characters. This works for numbers 10-100, however it allows a number from 0-9 to be input with four decimal places (i.e., 1.1235). Because the decimal place is set to three, the 1.1235 will round to 1.124, which is what I don't want. Is there any way to truncate the fourth decimal place or prevent more than three from being entered for numbers 0-9?

TOPICS
How to , JavaScript , PDF forms
1.0K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Mar 08, 2022 Mar 08, 2022

Try this as validation script:

var num = event.value;
if(isNaN(num)  || (num > 100 || num < 0)){
app.alert("Please enter number between 0-100.");
event.rc = false;}
else{
var dec = num.match(/^-?\d+(?:\.\d{0,3})?/)[0];
event.value = dec;}

View solution in original post

Translate
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 ,
Mar 08, 2022 Mar 08, 2022

Try this as validation script:

var num = event.value;
if(isNaN(num)  || (num > 100 || num < 0)){
app.alert("Please enter number between 0-100.");
event.rc = false;}
else{
var dec = num.match(/^-?\d+(?:\.\d{0,3})?/)[0];
event.value = dec;}

Translate
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
New Here ,
Mar 08, 2022 Mar 08, 2022

It worked! Thank you so much!

Translate
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 Beginner ,
Jan 02, 2024 Jan 02, 2024

I have a field that divides a number by 100 and I only want to show two places past the decimal without rounding, would this work for that and would I need to add this to the formula that does the dividing or would this go into a format tab?  

 

Code I was using in the calculate tab:

var grandTotal = +getField("GrandTotal").value;

if (!isNaN(grandTotal) && grandTotal !== 0) {

event.value = grandTotal / 100;
}

Code I was using in the format tab:

event.value = util.printf("%.2f", event.value);

 

Thank you in advance if you can help.

 

 

Translate
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 Beginner ,
Jan 02, 2024 Jan 02, 2024
LATEST

I have a field that divides a number by 100 and I only want to show two places past the decimal without rounding, would this work for that and would I need to add this to the formula that does the dividing or would this go into a format tab?  

 

Code I was using in the calculate tab:

var grandTotal = +getField("GrandTotal").value;

if (!isNaN(grandTotal) && grandTotal !== 0) {

event.value = grandTotal / 100;
}

Code I was using in the format tab:

event.value = util.printf("%.2f", event.value);

 

Thank you in advance if you can help.

 

Translate
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