JS float type conversion and take n decimal places

javascript variables are weakly typed, all the variables are declared as var, the type conversion process there is no java so convenient, it is through

parseInt (variable), parseFloat (variable) and other methods to carry out the type conversion. Note: No parseDouble (variable) This type of conversion, because

regardless of javascript in the single-precision and double precision float double, a small number of variables everything that is float, so to get the n decimal places, use the method toFixed (n) to get.

For example:
var f = 2.56556;
f.toFixed (2);

Preferable to the variable f 2 after the decimal, if not float type, but also the need for type conversion (parseFloat (variable name)), to obtain by this method.

分类:Web 时间:2010-03-27 人气:252
分享到:
blog comments powered by Disqus

相关文章

  • java data type conversion Integer String Long Float Double Date 2010-07-24

    1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Integer.valueOf (my_str). IntValue (); Note: The string converted to Double, Float, Lo

  • DB2 type conversion functions 2011-01-07

    Use CAST expression to convert the type Sql Code SELECT CAST (CURRENT TIME AS CHAR (8)) FROM SYSIBM.SYSDUMMY1 SELECT CAST(CURRENT TIME AS CHAR(8) ) FROM SYSIBM.SYSDUMMY1 1, converted to a string type: CHAR Function Syntax: CHAR (ARG) CHAR function re

  • struts2 type conversion study summary 2010-08-23

    Examples are given below 1, the preparation of input form input.jsp, the code below <body> <h2><font color="red">用逗号将两个点分开</font></h2> <s:form action="pointConverter"> <s:textfield name="point" label="point">

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

  • JavaScript Type Conversion 2010-09-27

    As a dynamic language, JavaScript's type conversion is always carried out in silence, not many people know. However, understanding the type of conversion is a good understanding of JavaScript JavaScript in the basic course. The conversion mechani

  • javascript numeric type conversion problems in the 2010-05-13

    Yesterday, a project, identified a problem, (0.3). ToString () value is not "0.3" and "0.30000000001", and (0.2). ToString () value is "0.2", very much puzzled. In that the string "0.3", so can only be (0.3). ToFixe

  • Javascript rounding decimal (n decimal places reserved rounded) 2010-10-28

    Javascript rounding decimal (n decimal places reserved rounded) First, problems arise: Own coding, javascript encountered in 3.21 * 3 = 9.629999999999999 phenomenon Second, Baidu, 1. ..... He is more clever will not add 0 after decimal <script>

  • in javascript how to get only two decimal places? 2009-08-16

    Cases sum = parseFloat (8.99) + parseFloat (7.50); Wait until the value is: 16.4900000002 How to get only two decimal places? <script language="JScript"> Number.prototype.toFixed = function (num) ( / / Re-structure toFixed method, IE5 ...

  • javascript decimal places for the treatment of retained 2010-10-12

    Transfer from: http://blog.csdn.net/lxh413/archive/2010/02/01/5276584.aspx Often encountered in the development process to adjust the decimal format, such as to retain two decimal places and so on. Method is also quite common, notes below. First, the

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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