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

http://21xp.com/kn6

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 .....

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

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

<script>
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 人气:226
分享到:
blog comments powered by Disqus

相关文章

  • 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

  • 几个常用的JavaScript字符串处理函数 - split().join().substring()和indexOf() 2014-05-14

    几个常用的JavaScript字符串处理函数 split().join().substring()和indexOf() 函数 :split() 功能 :使用一个指定的分隔符把一个字符串分割存储到数组 例子: str="jpg|bmp|gif|ico|png"; arr=theString.split("|"); //arr是一个包含字符值"jpg"."bmp"."gif"."ico"和&

  • javascript, java regular expressions refer to the practical application of reverse 2011-01-13

    I. Problem Description: The following string to href = "detail.jsp? Fruit = x3 & column = y5" replace onclick = "ClickChart ('x3', 'y5')" X3 and y5 is to come up with the two parameters as ClickChart <map name="filemap"

  • 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

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

CodeWeblog.com 版权所有 黔ICP备15002463号-1

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