Proceed to WirelessDevNet Home Page
Publications, e-books, and more! Community Tutorials Store Downloads, tools, & Freebies! IT Career Center News Home
newnav.gif

Newsletters
EMail Address:



   Content
  - Articles
  - Columns
  - Training
  - Library
  - Glossary
 
   Career Center
  - Career Center Home
  - View Jobs
  - Post A Job
  - Resumes/CVs
  - Resource Center
 
   Marketplace
  - Marketplace Home
  - Software Products
  - Wireless Market Data
  - Technical Books
 
   News
  - Daily News
  - Submit News
  - Events Calendar
  - Unsubscribe
  - Delivery Options
 
   Community
  - Discussion Boards
  - Mailing List
  - Mailing List Archives
 
   About Us
  - About WirelessDevNet
  - Wireless Source Disks
  - Partners
  - About MindSites Group
  - Advertising Information
 
WMLScript Reference WMLScript Reference

WMLScript > String > format()

format() - The format Function

Usage:  
format(format, value)
Description:  Converts the given value to a string by using the given formatting provided as a format string. The format string can contain only one format specifier, which can be located anywhere inside the string. If more than one is specified, only the first one (leftmost) is used and the remaining specifiers are replaced by an empty string. The format specifier has the following form:
% [width] [.precision] type
The width argument is a nonnegative decimal integer controlling the miniumum number of characters printed. If the number of characters in the ouput value is less than the specified width, blanks are added to the left until the minimum width is reached. The width argument never causes the value to be truncated. If the number of characters in the output value is greater than the specified width or, if width is not given, all characters of the value are printed (subject to the precision argument).

The precision argument specifies a nonnegative decimal integer, preceded by a period(.), which can be used to set the precision of the output value. The interpretation of this value depends on the given type:

d - Specifies the minimum number of digits to be printed. If the number of digits in the value is less than precision, the output value is padded on the left with zeroes. The value is not truncated when the number of digits exceeds precision. Default precision is 1. If precision is specified as 0 and the value to be converted is 0, the result is an empty string.

f - Specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Defalut precision is 6; if precision is 0 or if the period(.) appears without a number following it, no decimal point is printed.

s - Specifies the maximum number of characters to be printed. By default, all characters are printed.

Unlike the width argument, the precision argument can cause either truncation of the output value or rounding of a floating-point value.

The type argument is the only required format argument; it appears after any optional format fields. The type character determines whether the given value is interpreted as integer, floating-point or string. If the value argument is of a different type than is specified by the type argument, it is converted according to WMLScript standard automatic conversion rules, with the addition that if value is of type floating-point and type is d, Float.int() is called to convert the value. The supported type arguments are:

d - Integer: The output value has the form [-]dddddd, where dddd is one or more decimal digits.

f - Floating-point: The output value has hte form [-]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number and the number of digits after the decimal point depends on the requested precision. When the number of digits after the decimal point in the value is less than precision, letter 0 should be padded to fill columns (e.g. the result of String.format("%2.3f", 1.2) will be "1.200".

s - String: Characters are printed up to the end of the string or until the precision value is reached. When the width is larger than precision, the width should be ignored.

Any literal percent character(%) may be included in the format string by preceding it with another percent character(%%).


Example:
var a = 45;
var b = -45;
var c = "now";
var d = 1.2345678;
var e = String.format("e: %6d", a);        // e = "e:      45"
var f = String.format("%6d", b);           // f = "    -45"
var g = String.format("%6.4d", a);         // g = "  0045"
var h = String.format("%6.4d", b);         // h = " -0045"
var i = String.format("Do it %s", c);      // i = "Do it now"
var j = String.format("%3f", d);           // j = "1.2345678"
var k = String.format("%10.2f%%", d);      // k = "      1.23%"
var l = String.format("%3f %2f.", d);      // l = "1.2345678 ."
var m = String.format("%.0d", 0);          // m = ""
var n = String.format("%7d", "Int");       // n = invalid
var o = String.format("%s", true);         // o = "true"




Bookmark this page
Sponsors

Search

Eliminate irrelevant hits with our industry-specific search engine!









Wireless Developer Network - A MindSites Group Trade Community
Copyright© 2000-2010 MindSites Group / Privacy Policy
Send Comments to:
feedback@wirelessdevnet.com