Skip to main content
Participant
April 29, 2020
Question

Custom Format for percentage, how do i remove decimals?

  • April 29, 2020
  • 1 reply
  • 277 views

Hi all, long time reader, first time poster.

 

Thanks very much for all the help and info I've already managed to gather from here.

 

So onto my question:

I've used a custom format as a work around to the divide by zero errors. It works perfectly, but sometimes it will show 33% or 33.333% or even sometimes 33.3333% (33 just an example).

 

What can I add to my script to make it so it has no decimals? Or worst case scenario, only 1 or 2 decimal places?

Also, just thought of a side question, is it possible to have it show blank rather than NaN?

 

The custom format script I've used is:

 

var numerator = + getField ( " RMTimely " ) . value ;

var denominator = + getField ( " RMTotal " ) . value;

if ( denominator ! = = 0 ) {
event . value = numerator / denominator ;

} else {
event.value = " " ;

}

 

Any help or guidance you can give me on this is much appreciated.

 

Thanks in advance.

 

Kyle

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
April 29, 2020

Change this line:

event.value = numerator / denominator;

To this:

event.value = (numerator / denominator).toFixed(0);

 

If you're getting NaN that means that at least one of the values is not a real number.

KyleEganAuthor
Participant
April 29, 2020

Hi,

Thanks for the speedy response, appreciate you taking your time to answer.

 

So I've just changed the line you suggested. Unfortunately, I'm still having the same problem except now it always uses 4 decimal places.

 

Seems bizarre?

 

varnumerator=+getField("RMTimely").value;
vardenominator=+getField("RMTotal").value;
if(denominator!==0){
event.value=(numerator/denominator).toFixed(0);
}else{
event.value="";
}