The method of _javascript two digits after the decimal point of the float type JS technique

Source: Internet
Author: User
Tags pow

Here are some of the ways that JavaScript preserves two decimal places:
rounded
the following processing results are rounded:

var num =2.446242342;
num = num.tofixed (2); Output is 2.45

Not rounded
the following processing results are not rounded:
First, put the decimal edge integer:

Math.floor (15.7784514000 *)/+//  
output 15.77

The second type, as a string, uses a regular match:

Number (15.7784514000.toString (). Match (/^\d+?: \. \d{0,2})//  
output result is 15.77, cannot be used for integers such as 10 must be written as 10.0000

Note: If it is a negative number, convert it to a positive number before calculating and then back to negative

JavaScript retains an instance of two decimal places:

<script type= "Text/javascript" >//Reserved two decimal//function: Rounding floating-point numbers, taking 2-bit function ToDecimal (x) {var f after the decimal point 
      = parsefloat (x); 
      if (isNaN (f)) {return; 
      } f = Math.Round (x*100)/100; 
    return F; 
      The//system retains 2 decimal places, such as: 2, will be 00 after 2. That is, the 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 (POS))/math.pow (POS); 
    }//Rounded alert ("Keep 2 decimal places:" + todecimal (3.14159267)); 
    Alert ("Force retention of 2 decimal places:" + toDecimal2 (3.14159267)); 
    Alert ("Retain 2 decimal places:" + todecimal (3.14559267)); 
    Alert ("Force retention of 2 decimal places:" + toDecimal2 (3.15159267)); AlERT ("Retain 2 decimal places:" + fomatfloat (3.14559267, 2)); 
     
    Alert ("Retain 1 decimal places:" + fomatfloat (3.15159267, 1)); 
    Five homes six into 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 alert (3.1415.toExponential (2)); 
    Alert (3.1455.toExponential (2)); 
    Alert (3.1445.toExponential (2)); 
    Alert (3.1465.toExponential (2)); 
    Alert (3.1665.toExponential (1)); 
    Accurate to n-bit, excluding n-bit alert ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2)); 
    Alert ("Accurate to decimal point 3rd" + 3.1465.toPrecision (3)); 
    Alert ("Accurate to decimal point 2nd" + 3.1415.toPrecision (2)); 
    Alert ("Accurate to decimal point 2nd" + 3.1455.toPrecision (2)); 
  Alert ("Accurate to decimal point 5th" + 3.141592679287.toPrecision (5));  </script>

Use JavaScript to take float type two digits after the decimal point, example 22.127456 to 22.13, how to do?

1. Discard the fractional part and keep the whole number of parts

parseint (5/2)

2. Take up the whole, there are decimal numbers on the whole part plus 1

Math.ceil (5/2)

3, rounded.

Math.Round (5/2)

4, take the whole down.

Math.floor (5/2)

Alternative ways to

1. The Dumbest Way

Copy Code code as follows:

function Get ()
{
var s = 22.127456 + "";
var str = s.substring (0,s.indexof (".") + 3);
alert (str);
}

2. Regular expression works well

Copy Code code as follows:

<script type= "Text/javascript" >
onload = function () {
var a = "23.456322";
var anew;
var re =/([0-9]+.[ 0-9]{2}) [0-9]*/;
anew = A.replace (Re, "$");
alert (anew);
}
</script>

3. He is more intelligent ...

Copy Code code as follows:

<script>
var num=22.127456;
Alert (Math.Round (num*100)/100);
</script>

4. Friends who will use new things ... But it takes ie5.5+ to support it.

5.js Keep 2 decimal places (mandatory)

For decimal digits greater than 2 digits, use the above function is OK, but if less than 2 bits, such as: Changetwodecimal (3.1), will return 3.1, if you must need a format such as 3.10, then you need the following function:

Copy code code as follows:

function Changetwodecimal_f (x) {
var f_x = parsefloat (x);
if (isNaN (f_x)) {
Alert (' Function:changetwodecimal->parameter error ');
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;
}

Feature: Round floating-point numbers, 2 digits after the decimal point, or 0 if less than 2 digits,
This function returns the format usage of the string: Changetwodecimal (3.1415926) returns 3.14 Changetwodecimal (3.1) Returns 3.10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.