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

java script rounding to nearest 10

New Here ,
Dec 14, 2020 Dec 14, 2020

I have a form that needs to multiply two fields and then raound the answer to the nearest ten.

Below is what I have, can someone please tell me what I am missing

 

function DoubleRound(nDec, nValue) {
// Rounding function - round nValue to nDec decimal places
// use a double round to eliminate rounding error or Math.round
var nRound = Math.round(nValue / Math.pow(10, -nDec + 1)) * Math.pow(10, -nDec + 1)
return Math.round(nValue / Math.pow(10, -nDec)) * Math.pow(10, -nDec);
}

var u1= this.getField("TAXABLEVALUEOTHERPROPERTY").value;
var a1 = this.getField("RATIOOTHERPROPERTY").value;
event.value = ""; // default result if no divisor
if(Number(TAXABLEVALUEOTHERPROPERTY) != 0) {
// divisor not zero
var nResult = TAXABLEVALUEOTHERPROPERTY * RATIOOTHERPROPERTY; // compute division
nResult = DoubleRound(-1, nResult); // round to nearest 10
}

TOPICS
Comment markup and annotate , General troubleshooting , How to , PDF and browsers , PDF forms , Share or review PDFs , Windows
655
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
Community Expert ,
Dec 14, 2020 Dec 14, 2020
LATEST

I wrote a generic function that does it:

 

// mode 0 = round v to nearest multiple of x, 1 = round down, 2 = round up
function roundToNearestX(v, x, mode) {
	var mod = v%x;
	if ((mod>=(x/2) && mode==0) || mode==2) return v+(x-mod);
	if ((mod<(x/2) && mode==0) || mode==1) return v-mod;
}

 

For example: Calling roundToNearestX(16, 10, 0) will return 20.

 

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