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

Rounding issue

Participant ,
Aug 31, 2022 Aug 31, 2022

Copy link to clipboard

Copied

I'm struggling trying to resolve a rounding issue.  Basically on the form I set up, when to lines are added together sometimes because of rounding it throws off the total by one penny.  Really frustrated that I can figure out how to resolve it with Java Script.  Can anyone take a look at the script below and possibly offer any scripting that will give a result of two decimal places as an answer?  So, for example i may have result that comes up as 82.215.  In this case I'd like it to default a result to 82.22.  Here is the current script:

 

var theField = this.getField("l6A");
var theValue = theField.value;

if (theValue > 0.01) {
this.getField("l6").value= (theValue * 0.90);
}
else {
this.getField("l6").value="";
}

 

Thank you in advance for any ideas!

TOPICS
JavaScript

Views

602

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

Community Expert , Sep 05, 2022 Sep 05, 2022

Is off because your script is not employing the correct method.

 

If you're trying to keep a  number value to two decimal points, You must use the float format to handle the decimal fractions properly and in accordance with the decimal rounding rules. Otherwise, the term rounding, is being employed incorrectly in the context of your current script.

 

Change this line : 

 

this.getField("l6").value= (theValue * 0.90);

 

To:

 

this.getField("l6").value = util.printf("%.2f", (theValue * 0.90));

 

 

...

Votes

Translate

Translate
Community Expert ,
Aug 31, 2022 Aug 31, 2022

Copy link to clipboard

Copied

Did you format the field "l6" as a number with 2 decimals?

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
LEGEND ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Many people don't realise how demanding it can be to program for money calculations (at least, not until the tax authorities come to "investigate" a discrepancy). And I see you are on top of the result you need. 

what you need to do, to avoid rounding errors from partial pennies, is make sure EVERY figure is forced to a whole number of pennies when it is entered or calculated. You cannot fix this only on the final total. This is a lot of little scripts. Personally I would prefer to keep all figures in whole pennies like 1234 and only turn to 12.34 for display. Still need to round every input and calculation to a whole number. You might consider this too much. 

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
Participant ,
Sep 04, 2022 Sep 04, 2022

Copy link to clipboard

Copied

Unfortunately I'm being asked to keep it to 2 decimal points and because of third decimal makes it round up or down to the next cent it is throwing things off by a penny.

 

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 ,
Sep 05, 2022 Sep 05, 2022

Copy link to clipboard

Copied

Is off because your script is not employing the correct method.

 

If you're trying to keep a  number value to two decimal points, You must use the float format to handle the decimal fractions properly and in accordance with the decimal rounding rules. Otherwise, the term rounding, is being employed incorrectly in the context of your current script.

 

Change this line : 

 

this.getField("l6").value= (theValue * 0.90);

 

To:

 

this.getField("l6").value = util.printf("%.2f", (theValue * 0.90));

 

 

See additional tips in this other recent discussion:

 

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
Participant ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

AWSESOME!  That worked.  THANK YOU so much!

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 ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

LATEST

You're welcome.

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