Format()函数 sa WMLScript
The format() function formats a value.
Syntax
n = String.format(format, value)
Component | Description |
---|---|
n | The string returned by the function. |
format | Specify how to format the value. |
value | The value to be formatted. |
The format consists of three parts: %width.precision type
width - Optional. Specify the minimum number of characters to be output.
precision - Optional. Set the accuracy of the output value. The following values can be used:
- d - Minimum number of output digits. Default is 1.
- f - Number of decimal places. Default is 6.
- s - Maximum number of output characters. Default is to output all characters.
type - Required. Determine how to interpret the formatted value. The following values can be used:
- d - Integer
- f - Floating Point
- s - String
Example
var b = String.format("%4.3d", 32); var d = String.format("%3f", 10.1234); var e = String.format("%2.2f", 2.3)
Result
b = " 032" d = "10.123" e = "2.30"