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;
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of js number of decimal places retained

  • Hibernate primary key strategy-sequence

    Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...

  • hibernate call stored procedure

    hibernate call stored procedure

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries