js number of decimal places retained

javascript rounding decimal (n decimal places to retain rounded) 2009-05-12 15:44 javascript decimal rounding

First, problems arise:
Own coding, javascript encountered in 3.21 * 3 = 9.629999999999999 phenomenon

Second, Baidu about


Obtain the following information:

Javascript to take float type with two decimal places, eg to take into 22.13 22.127456, how to do?

1. The most stupid way ....... [me how dry .........]

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

2. Regular Expressions good results

<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, "$ 1");
alert (aNew);
</ Script>

3. He was more clever .....

var num = 22.127456;
alert (Math.round (num * 100) / 100);
</ Script>

4. Will use new friends ....... but things need to support IE5.5 + only.

var num = 22.127456;
alert (num.toFixed (2));
</ Script>

Third, sum, himself wrote a javascript multi-digit rounding the general method

/ / Num indicated that the number of rounded, v says that the number of decimal places.
function decimal (num, v)
var vv = Math.pow (10, v);
return Math.round (num * vv) / vv;

分类:Web 时间:2010-06-13 人气:238
blog comments powered by Disqus


  • javascript regular expressions Basic Usage 2009-03-01

    <script type="text/javascript"> window.onload = function(){ //RegExpInitialization ,gRefers to the global ,iIs case-insensitive var reg = new RegExp("cat","gi"); var exp = /cat/gi; //RegExpThe test ()Method ,The return

  • JS regular expressions (4) 2008-09-25

    Brackets (2) We have earlier discussed the issue once in brackets, see the following example: <script type="text/javascript"> function execReg(reg,str){ var result = reg.exec(str); alert(result); } reg = /^(b|c).+/; str='crazy.grass.com';

  • Verify upload the file extension of regular expressions 2010-07-09

    <script type="text/javascript"> function CheckFile(str) { var strRegex = "(.jpg|.JPG|.gif|.GIF)$"; // Used to verify that the picture file name extension, regular expressions var re=new RegExp(strRegex); if (re.test(str)){ return

  • Proficient in JS regular expressions 2010-07-26

    Regular expressions can be: • Test a string to a pattern. For example, an input string can be tested to see whether the string model there is a phone number or a credit card number pattern. This is called data validation • Replace the text. May in th

  • JS regular expressions API 2010-08-26

    What are regular expressions? This problem can be found: " Regular Expression Tutorial 30 minutes ", and many programming languages support regular expressions, this paper discussed only in JavaScript regular expressions. Create a regular expres

  • JS regular expressions (1) - test () method use 2010-12-15

    <script type="text/javascript"> var re = / se / i; / / declare a regular expression, all the matches he letter, if you want to ignore case, just add a i, (i said ignoreCase or case-insensitive) var sh = "SE"; / / Declare a string

  • Filter all HTML TAG code (regular expressions) 2010-02-12

    New Year's first task at work, responsible copied from the word in the content filtering out format (that is, filter out HTML tag), the use of js to achieve, in the paste key is pressed when the event trigger filter. Details of the code is as follows

  • javascript (JS) Regular Expressions 2010-07-23

    Check whether all the numbers function isDigit(s) { var patrn=/^[0-9]{1,20}$/; if (!patrn.exec(s)) return false return true } Check Login Name: Enter only 5-20 months beginning with the letter can be a string with digital ,"_","." func

  • Regular expressions to judge Chinese 2010-08-01

    1, determine whether a continuous string of Chinese characters (not including English and any other symbols and numbers): Regex.IsMatch ("Chinese ","^[ \ u4e00-\ u9fa5 ]+$"); 2, determine whether a string of Chinese string (only in Eng

  • Javascript interception of decimal places and rounded 2010-05-05

    round method Role: to return and give expression to the nearest integer value. Syntax: Math.round (number), will be option number parameter is rounded to the nearest integer value. Note: If the number is greater than equal to the fractional part of 0

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.072 (s). 12 q(s)