# Js to float two decimal places

• 2020-03-30 01:24:02
• OfStack

Here's how to implement JavaScript with two decimal places:
rounded
The following results will be rounded:

``````
var num =2.446242342;
num = num.toFixed(2); //  The output result is  2.45``````

No rounding
The following processing results will not be rounded:
First, round off the decimal:

``````
Math.floor(15.7784514000 * 100) / 100
//  The output result is  15.77``````

The second, as a string, USES regular matching:

``````
Number(15.7784514000.toString().match(/^d+(?:.d{0,2})?/))
//  The output result is  15.77, Cannot be used for integers such as  10  You must write to 10.0000``````

Note: if it is a negative number, convert it to a positive number and then calculate it

Javascript keeps instances of two decimal places:

``````
<script type="text/javascript">
//Keep two decimal places
//Function: round floating point number to 2 decimal places
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x*100)/100;
return f;
}

//The system retains two decimal places, such as: 2, which is followed by 00. That is, 2.00
function toDecimal2(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return false;
}
var f = Math.round(x*100)/100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}

function fomatFloat(src,pos){
return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
//rounded
alert(" keep 2 Decimal places: " + toDecimal(3.14159267));
alert(" Mandatory reserves 2 Decimal places: " + toDecimal2(3.14159267));
alert(" keep 2 Decimal places: " + toDecimal(3.14559267));
alert(" Mandatory reserves 2 Decimal places: " + toDecimal2(3.15159267));
alert(" keep 2 Decimal places: " + fomatFloat(3.14559267, 2));
alert(" keep 1 Decimal places: " + fomatFloat(3.15159267, 1));

//Six into five shekels
alert(" keep 2 Decimal places: " + 1000.003.toFixed(2));
alert(" keep 1 Decimal places: " + 1000.08.toFixed(1));
alert(" keep 1 Decimal places: " + 1000.04.toFixed(1));
alert(" keep 1 Decimal places: " + 1000.05.toFixed(1));

//Scientific count
//To n bits, no n bits
alert(" Accurate to the decimal point 2 position " + 3.1415.toPrecision(2));
alert(" Accurate to the decimal point 3 position " + 3.1465.toPrecision(3));
alert(" Accurate to the decimal point 2 position " + 3.1415.toPrecision(2));
alert(" Accurate to the decimal point 2 position " + 3.1455.toPrecision(2));
alert(" Accurate to the decimal point 5 position " + 3.141592679287.toPrecision(5));
</script> ``````

Using Javascript to get a float to two decimal places, for example 22.127456 to 22.13, how to do?

1. Discard the decimals and keep the integers

ParseInt (5/2)

2. Round up, add 1 to the whole number if there are decimals

Math. Ceil (5/2)

3. Round.

Math. Round (5/2)

4, round down

Math. Floor (5/2)

Alternative approach

The stupidest way

``````
function get()
{
var s = 22.127456 + "";
var str = s.substring(0,s.indexOf(".") + 3);
}

``````

2. Regular expressions work well

``````
<script type="text/javascript">
var a = "23.456322";
var aNew;
var re = /([0-9]+.[0-9]{2})[0-9]*/;
aNew = a.replace(re,"\$1");
}
</script>
``````

He's smarter...

``````
<script>
var num=22.127456;
</script>
``````

5. Js keep 2 decimal places (mandatory)

For decimal places larger than 2, the above function is fine, but for decimal places smaller than 2, such as changeTwoDecimal(3.1), will return 3.1.

``````
function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
return false;
}
var f_x = Math.round(x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
``````

Function: round the floating point number to 2 places after the decimal point. If there are less than 2 places, fill 0.
This function returns the format usage of the string: changeTwoDecimal(3.1415926) returns 3.14 changeTwoDecimal(3.1) returns 3.10

Related articles: