YUI 3.x Home -

YUI Library Examples: DataType Utility: DataType.Number.parse()

DataType Utility: DataType.Number.parse()

The Number module of the DataType Utility allows you to take a data value and convert it to a number.

To convert a data value to a number, simply call the parse() function of the DataType.Number class:

  1. YUI().use("datatype-number", function(Y) {
  2. var output = Y.DataType.Number.parse("123123.123");
  3. // output is the number 123123.123
  4. });
YUI().use("datatype-number", function(Y) {
    var output = Y.DataType.Number.parse("123123.123");
    // output is the number 123123.123
});

Under the hood, the data value is converted to a number via +data, not parseInt(). When the resulting value is NaN, then null is returned:

  1. YUI().use("datatype-number", function(Y) {
  2. var output = Y.DataType.Number.parse("$100");
  3. // output is null
  4.  
  5. output = Y.DataType.Number.parse("20 dollars");
  6. // output is null
  7.  
  8. output = Y.DataType.Number.parse(new Date("Jan 1, 2000"));
  9. // output is 946713600000
  10.  
  11. });
YUI().use("datatype-number", function(Y) {
    var output = Y.DataType.Number.parse("$100");
    // output is null
 
    output = Y.DataType.Number.parse("20 dollars");
    // output is null
 
    output = Y.DataType.Number.parse(new Date("Jan 1, 2000"));
    // output is 946713600000
 
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings