Skip to main content
rakeshk21205956
Inspiring
Sep 16, 2020 9:45 AM UTC
Answered

Adobe acrobat Form cannot capture "0" while using split() command

Hi,

I am trying to split a number (with 2 decimalsplace ) and separate them in before decimal and after decimal.

Example:  

.  Text1 :  I have number say  25.68  ,  now i want it to split into two fields so,

Text2 :  it should show 25 

Text3 : it should show 68   ,

everything is working fine.. until when the number after decimal is 00   then it doesnot show correct value;

 

below is my script

for Text2 calculation script :

var mins = this.getField("Text1").valueAsString;

event.value = mins.split(".")[0]

 

for Text3

var mins = this.getField("Text1").valueAsString;

event.value = mins.split(".")[1]

 

is there any other method to split the number where when the number has 00 in its decimal capture the 0 in Text3 .

This topic is closed to new replies. Start a new post to keep the conversation going.
rakeshk21205956
rakeshk21205956Correct answer
Inspiring
Sep 17, 2020 8:45 AM UTC

I would recommend against using this kind of construction:

var split = mins.toString().split(".")[1];

You're making an assumption there that there is a period in the string. If there isn't, your script will error out.

It's better to do it like this:

 

var minsParts = mins.toString().split(".");
if (minsParts.length==2) {
// there's one period in the string, handle as usual
} else {
// there's more than one period in the string, or none at all, handle as needed
}


ok.. thanks NesaNurani and try67   ...

script is working now... as required .. below is the script i used:

 

var mins = Number(this.getField("Text1").value);

var minsParts = mins.toString().split(".");

if (minsParts.length==2) {
minsParts[1];}
else {event.value =event.value = "00"  ;}

23 replies

Nesa Nurani
Community Expert
Community Expert
Sep 16, 2020 5:09 PM UTC

Just make sure field calculation order is like this:

and it should work fine.

 

Translate
Nesa Nurani
Community Expert
Community Expert
Sep 17, 2020 8:08 AM UTC

Sorry about my first post I thought you are just having troubles to make it work.

Anyway, you can try this workaround:

If result is whole number field "text3" will show 00 else it will split decimals normaly.

var mins = Number(this.getField("Text1").value);
var split = mins.toString().split(".")[1];
var int = Number.isInteger(mins);
if(int == true){
event.value = "00";}
else if(int == false){
event.value = split;}

It works for calculations so you don't need to enter value manually.

Hope that can help you 🙂

Translate
try67
Community Expert
Community Expert
Sep 17, 2020 8:19 AM UTC

I would recommend against using this kind of construction:

var split = mins.toString().split(".")[1];

You're making an assumption there that there is a period in the string. If there isn't, your script will error out.

It's better to do it like this:

 

var minsParts = mins.toString().split(".");
if (minsParts.length==2) {
// there's one period in the string, handle as usual
} else {
// there's more than one period in the string, or none at all, handle as needed
}

Translate
try67
Community Expert
Community Expert
Sep 16, 2020 9:53 AM UTC

That's because the ".00" is added by the Number Format script. It's not an actual part of the field's value.

You should first check if the value contains a ".". If so, split by it and proceed like before. If not, treat the whole value as you do the mins in Text2.

Translate
rakeshk21205956
Inspiring
Sep 16, 2020 10:13 AM UTC

there is no formatting done in Text1 .   06.00 is entered manually   Format tab is set to none

Translate
Bernd Alheit
Community Expert
Community Expert
Sep 16, 2020 11:19 AM UTC

Does you enter the value or does you use a calculation?

Translate
Bernd Alheit
Community Expert
Community Expert
Sep 16, 2020 9:49 AM UTC

What format does you use for field "Text3" ?

Translate
rakeshk21205956
Inspiring
Sep 16, 2020 10:11 AM UTC

no formatting for Text3

Translate
Bernd Alheit
Community Expert
Community Expert
Sep 16, 2020 10:42 AM UTC