JavaScript implements a variety of ways to preserve two-bit decimals _javascript tips

Source: Internet
Author: User
Tags pow reserved

The first method: The JavaScript implementation retains a two-digit decimal one-digit auto-complement 0 code instance:
The first method describes how to achieve a number of two-bit decimal effect, if the number of the original decimal number of less than two, then the missing automatically fill 0, this is also for the unified effect, first look at the code example:

function Returnfloat (value) {
 var value=math.round (parsefloat (value) *100)/100;
 var xsd=value.tostring (). Split (".");
 if (xsd.length==1) {
 value=value.tostring () + ".";
 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 code above implements our requirements, the following is an introduction to its implementation process.
Code comments:
1.function Returnfloat (value) {}, the parameter is the number to be converted.
2.var value=math.round (parsefloat (value) *100)/100, which should be the core of the function, parsefloat (value) converts the argument to a floating-point number, because the argument may be a string, Multiply by 100 because you want to keep two decimal digits, move the decimal point two digits to the right, and then use the Math.Round () method to do rounding, and then divide by 100, so that you have a reserved two decimal number and a rounded effect, but this is not perfect, If the number of decimal digits of the parameter itself is greater than or equal to 2, it is OK, such as 3.1415, but such as 3 or 3.0 is not a perfect implementation, keep looking below.
3.var xsd=value.tostring (). Split ("."), use dot "." Value is separated into an array.
4.if (xsd.length==1) {value=value.tostring () + "."; return value;}, if the length of the array is 1, that is, there is no decimal, then the number is added two 0, for example 3 is converted to 3.00.

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, such as a decimal, but the number of decimal digits is less than 2, that is, similar to 3.1 such, will be followed by a 0, that is, will be converted to 3.10.

The second method: A variety of methods for summarizing the functions of formatted data in JS to retain two decimal digits

Best way:

Keep the two-bit like this.

 var a = 9.39393; 
 Alert (a.tofixed (2));


Alert (number.tofixed (9.39393));
The return is 9.39
But only ie5.5 versions are supported.

Other methods:

Method One:

 function Roundfun (numberround,rounddigit)//rounded, reserved digits rounddigit  
 if (numberround>=0) 
 var tempnumber = parseint (Numberround * MATH.POW (10,rounddigit) +0.5))/math.pow (10,rounddigit); 
 return tempnumber; 
 var tempnumber = parseint (NumberRound1 * MATH.POW (10,rounddigit) + 0.5))/math.pow (10,rounddigit); 

Method Two:

 tmp = "1234567.57232" Result 
 = Tmp.substr (0,tmp.indexof (".") +3); 
 alert (result); 

A third way:JavaScript preserves two-bit decimal code

<script type= "Text/javascript" >//Reserved two decimal//function: Rounding floating-point numbers, taking 2-bit function after decimal point todecimal (x) {var f = Parsefl 
   Oat (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)); 

This is the JavaScript to achieve the retention of two decimal places a variety of methods, I hope to help you learn.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.