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

Multiply and rounding

New Here ,
Sep 11, 2018 Sep 11, 2018

I was trying to create a simple Proposal Form and I have run in to some issues. I've seen several places where there has been a code suggested but I don't know anything about that. I'm having trouble with decimals and rounding.  I have a few cells that I'm trying to calculate, I have "qty" "ourcost" "saleprice" "total". I'm taking "ourcost" divided by a constant cell of 0.1666667, this gives me my "saleprice". I then want to take the "saleprice" times the "qty" to give me "total". If any of that made sense here's the problem, i set the cells to only 2 decimals but when they are selected it shows 4 decimals, when saleprice shows 165.82 it's actually 165.8167, so if I have 2 of these items the total price should be 331.64 but it's showing 331.63 due to rounding.

TOPICS
PDF forms
627
Translate
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 11, 2018 Sep 11, 2018

You have just shown that the form field displayed value is not the actual value of the field. You need to apply rounding to the actual computed value.

One can use the "util.printf" method to round a value to a given number of decimal places.

A rounding function could look something like:

function utilRound(nValue, nDec) {

return Number(util.printf("%,101." + nDec + "f", nValue));

}

In financial calculations it is best to round early and often. Sales tax accumulations can get real fussy about not being rounded.

Translate
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
New Here ,
Sep 12, 2018 Sep 12, 2018

Capture1.PNGCapture2.PNGCapture3.PNG

I appreciate the response but I do not know how to write code or even where to put it. I've tried to copy a few codes and manipulate them to see if they work because the issue seemed very similar to mine but they didn't work.

I have 2 calculations I am doing in this form. In the first picture you can see 99.49 which is my cost, the 165.82 is calculated from my cost multiplied by a dummy cell of 1.666667 (markup) to get my sale price, so far no problems, this would be great if I stopped here. Now the problem comes in the second calculation when I take the sale price of 165.82 times the QTY, it gives me 331.63 because of the rounding when it should be 331.64. There are 13 lines I want to make this same calculation for. Something I thought was going to be pretty simple to create has become a challenge.

Translate
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 12, 2018 Sep 12, 2018

Performing math on computers has never been simple. Computers do math with numbers with base 2 or binary, while we generally do math in base 10 or the decimal system. Whole numbers convert to binary values with no issue, but the fractional or decimal values from base 10 to do not convert to nice binary values. Also a previously noted, the computed value for the computer may result in an irrational number which we would convert unconsciously round to 2 decimal places. Your example of 99.49 * 1.666667 results in a value of 165.81669982999998, And when that value is multiplied by the quantity of 2 one has a result of 331.63339965999995. When a formatting of Number an 2 Decimals is applied the displayed value is 331.63. However if one rounds the field value of the Sales Price to 165.82 and multiplies that actual value by the quantity of 2, one gets the desired value of 331.64. Therefore one may need to round the actual value of a field in order to come to the correct or expected value.

See Computations With and Without Rounding for a working example of your example. When you tab through the fields you will see some fields display a different value than what is displayed if the field is not selected. This is the actual value of the field and not the requested formatted value of the field.

Translate
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 12, 2018 Sep 12, 2018
LATEST

Yes, programming is a lifetime challenge. Fun though. But don't expect to learn it quickly.

Translate
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