Highlighted

display number with two decimals only if not interger

Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Hello,

 

I'm doing some operations with numbers and I want to display the final value in a text field as:

if the number is integer without decimals 100;200;32;15 etc

if the number is not integer with two decimals. 99.12; 23.11 etc

 

Now I'm using 

this.getField("textfield").value=Number(result).toFixed(2);

but it always displays decimals - like 100.00 etc.

 

Is there a function or format setting to accomplish this?

Thank you very much.

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

You can use:

this.getField("textfield").value=Number(result).toFixed(2).replace(".00", "");

TOPICS
PDF forms

Views

42

Likes

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

display number with two decimals only if not interger

Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Hello,

 

I'm doing some operations with numbers and I want to display the final value in a text field as:

if the number is integer without decimals 100;200;32;15 etc

if the number is not integer with two decimals. 99.12; 23.11 etc

 

Now I'm using 

this.getField("textfield").value=Number(result).toFixed(2);

but it always displays decimals - like 100.00 etc.

 

Is there a function or format setting to accomplish this?

Thank you very much.

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

You can use:

this.getField("textfield").value=Number(result).toFixed(2).replace(".00", "");

TOPICS
PDF forms

Views

43

Likes

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
Nov 19, 2020 0
Engaged ,
Nov 19, 2020

Copy link to clipboard

Copied

Try like this:

this.getField("textfield").value = Math.round((result)*100)/100;

EDIT: if there is more then 2 decimals it will round to 2 but if there is 1 it will show 1, if you still want to show 1 decimal as 2(100.2 as 100.20) use like this:

this.getField("textfield").value = (Math.round((result)*100)/100).toFixed(2);

Likes

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
Reply
Loading...
Nov 19, 2020 1
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

When there is ". 00" at the end you can remove it. 

Likes

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
Reply
Loading...
Nov 19, 2020 1
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Good idea. Thanks.

Likes

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
Reply
Loading...
Nov 19, 2020 0
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Can you please recommend a solution on how to do it? I'm thinking of using the split() function. Thanks.

Likes

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
Reply
Loading...
Nov 19, 2020 0
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

Here is some simple, and very direct code to use in the Custom Format script.

 

if(!Number.isInteger(event.value))

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

 

It uses the built-in JavaScript function for testing if a number is an integer. If it is not an integer, then the number if formatted with 2 decimal places.  The correct way to format  numbers is with the "util.printf" function. 

 

 

Likes

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
Reply
Loading...
Nov 19, 2020 1
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

This always shows 2 decimals. I added the code in the Custom Format script section for the textfield.

I have a button click - on button click I set 

this.getField("result").value = 2;

 

The result is 2.00. maybe I'm missing something

 

 

Likes

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
Reply
Loading...
Nov 19, 2020 0
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

Where does this number come from?  If it is the result of a caculation, then it is likely to always be a decimal number, even if the result does not have any decimal places, i.e., it is never an integer. 

So what you really want to detect is a 0 decimal value. 

 

So do it this way. 

if(/\.(\d+)/.test(event.value) && (Number(RegExp.$1)>0))
  event.value = util.printf("%.2f",event.value);
else
  event.value = util.printf("%d",event.value);

Likes

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
Reply
Loading...
Nov 19, 2020 0
oanass LATEST
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

>>Where does this number come from?

For testing, I just used

this.getField("Textfield").value = 2; on a button click.

 

The new code is working fine. Thank you!

Likes

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
Reply
Loading...
Nov 19, 2020 0
Adobe Community Professional ,
Nov 19, 2020

Copy link to clipboard

Copied

You can use:

this.getField("textfield").value=Number(result).toFixed(2).replace(".00", "");

Likes

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
Reply
Loading...
Nov 19, 2020 2
Community Beginner ,
Nov 19, 2020

Copy link to clipboard

Copied

Perfect! Thank you.

Likes

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
Reply
Loading...
Nov 19, 2020 0
Contributor ,
Nov 19, 2020

Copy link to clipboard

Copied

That is not even close to what you ask in your post, next time explain better what you actually need.

Likes

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
Reply
Loading...
Nov 19, 2020 0