Skip to main content
Known Participant
December 11, 2024
Question

How can I have 2 different decimal number formats in the same field.

  • December 11, 2024
  • 8 replies
  • 955 views

Time to ask the experts for help again.

I have 2 text fields. The first is an initial value and the second field is the value after the device is adjusted. If the first field is within a range, the second field populates with 'N/A'. Both fields will always be a decimal number starting with zero (i.e. 0.049 or 0.0493). This number must have 3 decimal places or 4 decimal places and must have the leading zero. The second field must have the same number of decimal places as the first.

I know I can use the Arbitrary Mask (0.9999) or (0.999) to achieve 1 of the results but is it possible (with code) to achieve both options.
The requirements are;
The number must start with 0. (zero and a decimal point).
The number can only have either 3 or 4 decimal places.
The second field must have the same decimal places as the first field. (I think I have the correct code in the second field already for this)


I initially had no formatting in the first field, apart from the limit to the number of characters. The script initially came about because when someone was testing it, they were not inputting the leading zero (i.e .049). So I was tasked to fix it. Personally, I think that users should just be told to enter it the correct way but here we are.

In the first field, I currently have a limit of 6 characters set in the option field and a custom keystroke script (see below). That sort of works but allows the user to enter any value up to 6 digits including the decimal point.

 

if (event.change) event.rc = /^[0-9,\.]+$/.test(event.change);

 

If possible, how would I modify it to suit the requirements above. We could probably live with the leading zero being any digit if that is easier.

----------------------------------------------------------
In the second field, I have the code below, which does work except as soon as I tab into it, it shows 0.000 or 0.0000 (dependant on the first field). I would rather it remains blank until the value is being entered. I did try this as validate code but it did not work.

How can I avoid the zeros appearing. If there is code to solve for formatting field 1, I would also use it in this field


var ff = this.getField("FirstField").valueAsString;
if (Number(ff)>=0.0380 && Number(ff)<=0.0420){
event.value = "N/A";
event.target.textColor = color.black;
}
else if (ff!="" && event.value.length!=0 && (event.value<0.0380 || event.value>=0.0420)) {
event.target.textColor = color.red;
app.alert("WARNING: The value is outside the range of 0.0380 to 0.0420")
}
else if (ff.length==6){
event.value = util.printf("%1.4f",event.value);
event.target.textColor = color.black;
}
else if (ff.length==5){
event.value = util.printf("%1.3f",event.value);
event.target.textColor = color.black;
}
else if (ff==""){
event.value = "";
event.target.textColor = color.black;
}

 

Thanks for any assistance you can provide.

This topic has been closed for replies.

8 replies

try67
Community Expert
Community Expert
December 11, 2024

Under which event did you place the last code?

Known Participant
December 11, 2024

I placed it in the second field as a custom calculation script.

 

try67
Community Expert
Community Expert
December 11, 2024

And what did you select under Format?