• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• How to set a number to only show three decimal pla...

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

New Here ,
Mar 08, 2022 Mar 08, 2022

Copied

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

Views

471

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

Try this as validation script:

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

4 Replies 4
Community Expert ,
Mar 08, 2022 Mar 08, 2022

Copied

Try this as validation script:

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

Report

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

Copied

It worked! Thank you so much!

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

Copied

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.

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

Copied

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.