

2. Re: division javascript
try67 Apr 7, 2015 4:40 AM (in response to hasand59089870)No, actually. You have to first make sure that the divisor is not 0 or you'll get error messages because division by zero is not permitted.
So the last line should be changed to:
if (v2==0) event.value = "";
else event.value = v1/v2;

3. Re: division javascript
klothos Apr 7, 2015 4:44 AM (in response to hasand59089870)thanks but i would like the result to be only two decimal .
but is like this 53.32564879999 .I want 53.32

4. Re: division javascript
try67 Apr 7, 2015 5:14 AM (in response to klothos)Just set the field's Format setting to Number with 2 decimals, then.

5. Re: division javascript
hasand59089870 Apr 7, 2015 5:42 AM (in response to klothos)if the formatting of the fields is not possible, than try this:
var v1 = +getField("B19").value;
var v2 = +getField("Textfield37").value;
var c = v1/v2
event.value = (v2 != 0) ? (v1/v2).toFixed(2) : ""

6. Re: division javascript
try67 Apr 7, 2015 5:44 AM (in response to hasand59089870)toFixed will work too, but what's the point of the c variable in the code above?

7. Re: division javascript
hasand59089870 Apr 7, 2015 5:52 AM (in response to try67)Forget c. i have needed for my testing
var v1 = +getField("B19").value;
var v2 = +getField("Textfield37").value;
event.value = (v2 != 0) ? (v1/v2).toFixed(2) : ""

8. Re: division javascript
gkaiseril Apr 7, 2015 11:25 AM (in response to klothos)So you do not want rounding of the result.
For the result without rounding I would use the Math.floor method for truncating the value as needed.
function Truncate(nValue, nDed) {
// truncate a value to number of decimal places;
var nDecAdjust = Math.pow(10, nDec); // decimal adjustment value;
var nResult = nValue * nDecAdjust; // shift decimal point for truncation;
nResult = Math.floor(nResult); // truncate to intiger;
nResult = nResult / nDecAdjust; // sift decimal point back;
return nResult; // return truncated value;
}var v1 = +getField("B19").value;
var v2 = +getField("Textfield37").value;
var nDec = 2;
event.value = v2 != 0 ? Truncate(v1/v2, nDec) : "";>> thanks but i would like the result to be only two decimal .
but is like this 53.32564879999 .I want 53.32
This would truncate the value at 2 decimal places and not round to 2 decimal places.
Using "toFixed(2)" provides the rounding and results in a value of 53.33 since the next digit in the answer is 5 and one rounds up at 5 or greater. To obtain the answer of 53.32 one would need to truncate the value at 2 decimal places.
There is no "truncation" function as such in JavaScript but one can write one. The Truncation function provided could be shortened but I have expanded it to show more clearly show the details of the calculation logic.
This method sets the actual value of the calculation to 2 decimal places.
Using the format tab option only sets the display value of the field rounded to the specified number of decimal palaces and not the actual value of the field.