Home > Web Front-end > JS Tutorial > body text

JavaScript rounding function code sharing (retaining the last few digits)_javascript skills

WBOY
Release: 2016-05-16 17:09:53
Original
1133 people have browsed it

Let’s just look at the code

Parameter 1: Number to retain decimal places

Parameter 2: Number of digits to retain


Copy code The code is as follows:

function FormatNumber(srcStr,nAfterDot){
var srcStr,nAfterDot;
var resultStr,nTen;
srcStr = "" srcStr "";
strLen = srcStr.length;
dotPos = srcStr.indexOf(".",0) ;
if (dotPos == -1){
resultStr = srcStr ".";
for (i=0;i resultStr = resultStr "0";
}
}
else{
if ((strLen - dotPos - 1) >= nAfterDot){
nAfter = dotPos nAfterDot 1;
nTen =1;
for (j=0;j nTen = nTen*10;
}
resultStr = Math.round(parseFloat(srcStr)*nTen)/nTen;
}
else{
resultStr = srcStr;
for (i=0;i<(nAfterDot - strLen dotPos 1);i ){
resultStr = resultStr "0";
}

}
}

return resultStr;

}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template