JavaScript parsefloat Date Format instance

Source: Internet
Author: User
  code is as follows copy code

function Formatnum (prmnum,prmptn,prmnullvalue) {
var nullvalue = Prmnullvalue? prmnullvalue:0;
if (Prmnum = = "" | | Prmnum = = null) return nullvalue;
Prmnum = Prmnum.replace (/,/g, "");
if (isNaN (prmnum)) return nullvalue;
if (PRMPTN = = "" | | PRMPTN = = null) return prmnum;
var ptnlen = prmptn.length;
var pointindex = Prmptn.indexof (".");
var groupindex = Prmptn.indexof (",");
var Pointlen = (PointIndex = = 1 0:ptnlen-pointindex-1);
Prmnum = parsefloat (prmnum). tofixed (Pointlen) + "";
if (Groupindex!=-1) {
if (PointIndex = = 1) pointindex = Ptnlen;
var len = pointindex-groupindex-1;
var reg = new RegExp ("(-?d+) (d{" +len+ "})");
while (Reg.test (prmnum)) Prmnum = Prmnum.replace (Reg, "$1,$2");
}
return prmnum;
}

Definitions and usage

The parsefloat () function parses a string and returns a floating-point number.

Grammar

Parsefloat (String)
Parameters Description
X Necessary. The string to be parsed.

return value

Returns the parsed number.

Tips and comments

Note: only the first digit in the string is returned.

Note: the spaces at the beginning and end are allowed.

Tip: If the first character of a string cannot be converted to a number, then parsefloat () returns Nan.

Tip: If you want to resolve only the integer portion of a number, use the parseint () method.

Instance

In this case, we'll use parsefloat () to parse the different strings:

<script type= "text/web Effects" >

document.write (parsefloat ("ten")) 
document.write (parsefloat ("10.00")) 
document.write (parsefloat ("10.33")) 
document.write (parsefloat ("a") 
document.write ( Parsefloat ("a")) 
document.write (parsefloat ("Years"))
document.write (parsefloat ("he"))

< /script>

Output:

10.33
nan

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.