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

Variable decimal places

Guest
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

I've tried this a variety of ways at this point in time.

I had my base code working, but realized that the rounding was impacting the caculation.  So now I'm trying to have the caculation based on a fixed number of decimal places.  However, depending on the selection from the field contract type it could be either 2 or 4 decimal places. 

This is where I am currently.... 

var CYP = this.getField("CY Plus CPI").value; //CY Plus CPI

var drop = this.getField("Contract type")  // used to determine number of decimal places
if(drop == "Cost Reimbursable Contract")  // only 2 otherwise 4
var RY = this.getField("RY Food").number.toFixed(2);
else RY = this.getField("RY Food").number.toFixed(4);

{if(RY>CYP) event.value = "needs SFA review";
else event.value = "ok";}

So if RY is greater the CYP it needs to return needs SFA review otherwise its okay.  In one type the RY is only out to 2 decimal and in the other its out to 4.

 

I've tried this....but no luck.  But I think it had an error I couldn't figure out.

var RY = this.getField("RY Food").value; // RY
var CYP = this.getField("CY Plus CPI").value; //CY Plus CPI

var drop = this.getField("Contract type")  // used to determine number of decimal places
if(drop == "Cost Reimbursable Contract")  // only 2
{if (RY.toFixed(2)>CYP) event.value = "needs SFA review";
else event.value = "ok";}
 
if(drop == "Fixed Price Contract")  // only 4
{if(RY.toFixed(4)>CYP) event.value = "needs SFA review";
else event.value = "ok";}

 

I think I'm close on the 1st one.  The field is OK Food.

I'm stumped.

 

Clueless Beginner

TOPICS
JavaScript , PDF forms

Views

864

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

Deleted User
Jan 06, 2023 Jan 06, 2023

Well I am a clueless beginner and a digital immigrant.  I am not trained in how to write JavaScript but am self taught.

 

Here is the code that worked.

var CYP = this.getField("CY Plus CPI").valueAsString; //CY Plus CPI

var drop = this.getField("Contract type").value;  // used to determine number of decimal places
if(drop == "Cost Reimbursable Contract")  // only 2 otherwise 4
var RY = Number(this.getField("RY Food").valueAsString).toFixed(2);
else var RY = Number(this.getField("RY Food").valueAsS
...

Votes

Translate

Translate
Community Expert ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

You are missing a red part:

var drop = this.getField("Contract type").value

Also, what are you trying to compare here:

if (RY.toFixed(2)>CYP)
if(RY.toFixed(4)>CYP)

 

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, 2023 Jan 03, 2023

Copy link to clipboard

Copied

That's not the only issue. These two lines are also incorrect, since there's no "number" property for fields:

var RY = this.getField("RY Food").number.toFixed(2);
else RY = this.getField("RY Food").number.toFixed(4);

Instead, they should use:

var RY = Number(this.getField("RY Food").valueAsString).toFixed(2);
else RY = Number(this.getField("RY Food").valueAsString).toFixed(4);

 

Another issue is that the toFixed method returns a string object, not a number, so comparing them using the mathematical operators will not work as expected.

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
Guest
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

So how would I compare them as either 2 decimal or 4 decimal places if .toFixed won't work with mathematical operators?

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, 2023 Jan 03, 2023

Copy link to clipboard

Copied

Use the Number constructor on them again, to convert them back into numbers.

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
Guest
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

Is this what you mean?

var CYP = this.getField("CY Plus CPI").valueAsString; //CY Plus CPI

var drop = this.getField("Contract type")  // used to determine number of decimal places
if(drop == "Cost Reimbursable Contract")  // only 2 otherwise 4
var RY = number(this.getField("RY Food").valueAsString).toFixed(2);
else RY = number(this.getField("RY Food").valueAsString).toFixed(4);

{if (number(RY)>CYP) event.value = "needs SFA review";
else event.value = "ok";}

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, 2023 Jan 03, 2023

Copy link to clipboard

Copied

It's Number(), not number(), and you didn't fix the first mistake pointed out by Nesa.

And you should also convert CYP to a number, using the same method.

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
Guest
Jan 06, 2023 Jan 06, 2023

Copy link to clipboard

Copied

LATEST

Well I am a clueless beginner and a digital immigrant.  I am not trained in how to write JavaScript but am self taught.

 

Here is the code that worked.

var CYP = this.getField("CY Plus CPI").valueAsString; //CY Plus CPI

var drop = this.getField("Contract type").value;  // used to determine number of decimal places
if(drop == "Cost Reimbursable Contract")  // only 2 otherwise 4
var RY = Number(this.getField("RY Food").valueAsString).toFixed(2);
else var RY = Number(this.getField("RY Food").valueAsString).toFixed(4);

if ((parseInt(RY))>(parseInt(CYP))) event.value = "needs SFA review";
else event.value = "ok";

 

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