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

Truncate numbers without rounding

Community Beginner ,
Jan 02, 2024 Jan 02, 2024

Copy link to clipboard

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.

 

TOPICS
General troubleshooting , How to , JavaScript , PDF , PDF forms

Views

145

Translate

Translate

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 guidelines

correct answers 1 Correct answer

Community Expert , Jan 03, 2024 Jan 03, 2024

If you only need to do it once then the full calculation code can be:

 

var grandTotal = Number(this.getField("GrandTotal").value);
if (!isNaN(grandTotal) && grandTotal !== 0) {
	event.value = truncateNumber(grandTotal / 100, 2);
} else event.value = "";

function truncateNumber(v, nDecimals) {
	var re = new RegExp("\\d+\\.\\d{"+nDecimals+"}");
	var results = (""+v).match(re);
	if (results!=null) return results[0];
	else return v;
}

Votes

Translate

Translate
Community Expert ,
Jan 02, 2024 Jan 02, 2024

Copy link to clipboard

Copied

Don't use the Format tab at all for this.

Instead, you can use this function I wrote in your calculation script:

 

function truncateNumber(v, nDecimals) {
	var re = new RegExp("\\d+\\.\\d{"+nDecimals+"}");
	var results = (""+v).match(re);
	if (results!=null) return results[0];
	else return v;
}

 

If you define it as a doc-level script, change this line in your code:

event.value = grandTotal / 100;

To this:

event.value = truncateNumber(grandTotal / 100, 2);

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jan 03, 2024 Jan 03, 2024

Copy link to clipboard

Copied

I am new to working with JavaScript, so sorry if this is a dumb question but would I use your code as a stand-alone in place of the one I made?

Votes

Translate

Translate

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 guidelines
Community Expert ,
Jan 03, 2024 Jan 03, 2024

Copy link to clipboard

Copied

LATEST

If you only need to do it once then the full calculation code can be:

 

var grandTotal = Number(this.getField("GrandTotal").value);
if (!isNaN(grandTotal) && grandTotal !== 0) {
	event.value = truncateNumber(grandTotal / 100, 2);
} else event.value = "";

function truncateNumber(v, nDecimals) {
	var re = new RegExp("\\d+\\.\\d{"+nDecimals+"}");
	var results = (""+v).match(re);
	if (results!=null) return results[0];
	else return v;
}

Votes

Translate

Translate

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 guidelines