This article mainly introduces multiple methods to retain two decimal places in javascript. If the original decimal places of a number are less than two decimal places, the missing digits are automatically set to zero. If you are interested, please refer
Method 1:
Javascript to retain two decimal places to automatically add zero code example:
The first method introduces how to retain two decimal places for a number. If the original decimal places of a number are less than two decimal places, the missing digits are automatically filled with zero, which is also for the purpose of unified effect, first look at the code instance:
function returnFloat(value){
var value=Math.round(parseFloat(value)*100)/100;
var xsd=value.toString().split(".");
if(xsd.length==1){
value=value.toString()+".00";
return value;
}
if(xsd.length>1){
if(xsd[1].length<2){
value=value.toString()+"0";
}
return value;
}
}
var num=3.1;
console.log(returnFloat(num));
The above code implements our requirements. The following describes the implementation process.
Code comment:
1.Function returnFloat (value ){},The parameter is the number to be converted.
2.Var value = Math. round (parseFloat (value) * 100)/100,This should be the core of the function. parseFloat (value) converts the parameter to a floating point number, because the parameter may be a string, multiplied by 100 because two decimal places are reserved, first move the decimal point to the right two digits, and then use Math. the round () method performs rounding calculation and is finally divided by 100. In this way, two decimal places are retained and the rounding effect is achieved, but this is not perfect, if the decimal point of the parameter number is greater than or equal to 2, it is acceptable, for example, 3.1415, but there is still no perfect implementation such as 3 or 3.0. Let's continue to look at the following.
3. var xsd = value. toString (). split ("."), which is separated into an array by Dot "." value.
4. if (xsd. length = 1) {value = value. toString () + ". 00 "; return value;}. If the length of the array is 1, that is, there is no decimal number, two zeros are added for this number. For example, 3 is converted to 3.00.
5.
if(xsd.length>1){
if(xsd[1].length<2){
value=value.toString()+"0";
}
return value;
}
If (xsd. length> 1) is used to determine whether the length of a number is greater than 1, that is, whether the number has a decimal number. If there is a decimal number, but the decimal number is less than 2, that is, like 3.1, A 0 value is added to the end, that is, the value is converted to 3.10.
Method 2:Multiple methods to collect functions for saving two decimal places for formatting data in JS
Best method:
Keep the two. It seems like this is the case.
var a = 9.39393; alert(a.toFixed(2));
Note:
Alibaba (Number. toFixed (9.39393 ));
9.39 is returned.
However, only ie5.5 or later versions are supported.
Other methods:
Method 1:
function roundFun (numberRound, roundDigit) // Rounding, keep the number of roundDigit
{
if (numberRound> = 0)
{
var tempNumber = parseInt ((numberRound * Math.pow (10, roundDigit) +0.5)) / Math.pow (10, roundDigit);
return tempNumber;
}
else
{
numberRound1 = -numberRound
var tempNumber = parseInt ((numberRound1 * Math.pow (10, roundDigit) +0.5)) / Math.pow (10, roundDigit);
return -tempNumber;
}
}
Method 2:
《script》
tmp = "1234567.57232"
result = tmp.substr(0,tmp.indexOf(".")+3);
alert(result);
《script》
Method 3: javascript retains two decimal places