YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataType Utility: DataType.Number.format()

## DataType Utility: DataType.Number.format()

The Number module of the DataType Utility allows you to take a number and format it into a string with the following configuration parameters: `decimalPlaces`, `decimalSeparator`, `thousandsSeparator`, `prefix`, and `suffix`.

View example in new window.

Number to format:
Thousands separator: Decimal separator: Decimal places:
Prefix: Suffix:
:

To output a number as a string, simply call the `format()` function of the DataType.Number class:

    YUI().use("datatype-number", function(Y) {    alert(Y.DataType.Number.format(123123123));     // This alerts the string "123123123"});

    YUI().use("datatype-number", function(Y) {
        alert(Y.DataType.Number.format(123123123));

        // This alerts the string "123123123"
    });

YUI().use("datatype-number", function(Y) { alert(Y.DataType.Number.format(123123123)); // This alerts the string "123123123" });
The `format()` function also takes a variety of configuration parameters that manipulate the value of the string. This is helpful, for instance, when displaying numbers as currency:

    YUI().use("datatype-number", function(Y) {    alert(Y.DataType.Number.format(123123123.176,{        prefix: "&amp;#165;",        thousandsSeparator: ".",        decimalSeparator: ",",        decimalPlaces: 2,        suffix: " (YEN)"    }));     // This alerts the string "&#165;123.123.123,18 (YEN)"});

    YUI().use("datatype-number", function(Y) {
        alert(Y.DataType.Number.format(123123123.176,{
            prefix: "&amp;#165;",
            thousandsSeparator: ".",
            decimalSeparator: ",",
            decimalPlaces: 2,
            suffix: " (YEN)"
        }));

        // This alerts the string "&#165;123.123.123,18 (YEN)"
    });

YUI().use("datatype-number", function(Y) { alert(Y.DataType.Number.format(123123123.176,{ prefix: "&#165;", thousandsSeparator: ".", decimalSeparator: ",", decimalPlaces: 2, suffix: " (YEN)" })); // This alerts the string "¥123.123.123,18 (YEN)" });

Copyright © 2009 Yahoo! Inc. All rights reserved.

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