JS format number reserved Two-bit decimal point example code

Source: Internet
Author: User

Problem: Multiple ways to format data in JS to preserve two-bit decimal functions

The best way:

Keep two bits like that.

Copy the Code code as follows:

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


Alert (number.tofixed (9.39393));

The return is 9.39.
However, only ie5.5 versions are supported.

Other methods:

functionroundFun(numberRound,roundDigit) //四舍五入,保留位数为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 Two:

<script>   tmp = "1234567.57232"   result = tmp.substr(0,tmp.indexOf( "." )+3);   alert(result);



var a=3.1415926;
A = a.tofixed (2);//reserved 2 bit but result is a string type
A = parsefloat (a);//convert result to float
In one step, the following
A = Parsefloat (a.tofixed (2));

This article is from the "Liu Bofang blog" blog, make sure to keep this source http://liubofang.blog.51cto.com/11162557/1875830

JS format number reserved Two-bit decimal point example code

Related Article

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.