Formatting Output |
Top Previous Next |
While its easy enough to use PRINT and SETPRECISION to control how numbers are displayed in a window, or console. Sometimes more advanced formatting is necessary. The USING/WUSING functions return a string that is formatted based on a specifier string and one or more parameters. The syntax of the USING function is: USING( formatstring, param1 {,param2...} ) WUSING( formatstring, param1 {,param2...} ) The format string is a string literal or variable that can contain special formatting symbols as well as regular text to be inserted into the final output string. USING understands the following format specifiers:
An example format string would look like "#,###.##" which would reserve two places to the right of the decimal place and four to the left. The result string would also have comma's inserted between every third and forth digit. The output can be used as a parameter to any function that accepts a string, assigned to a string variable or used with the PRINT statement. PRINT USING("$#,###.##", 1145.551) Would produce the output of: $1,145.56 $ 22.80 Note that the '$' is copied to the result string directly. The comma does not need to be placed correctly in the format string. It just needs to be somewhere in the definition. PRINT USING("#,######", 1234567) Produces the output of: 1,234,567 Truncation and rounding USING will not truncate the output if the number of digits exceeds the number of # symbols on the left of the decimal point. Make sure you have enough # symbols to accommodate the output width. The right side of the decimal point is always rounded and truncated to match the format definition. PRINT USING("##.###", 5115.1234) Produces the output of: 5115.123 Filling with spaces or 0 The left side of the decimal point determines how many spaces or 0's to use as a fill value if there are not enough digits to fill the field. PRINT USING("0######", 23) Produces the output of: 000023 23 To left justify the output in the field use a minus sign (-) as a leading character. PRINT USING("-#### -####", 55, 88) Produces the output of: 55 88 Including string variables To copy the contents of a string literal or variable use the & symbol in your definition PRINT USING("&$#,###.##&", "The total cost is ", 3000.55, " Dollars") Produces the output of: The total cost is $3,000.55 Dollars Expected types and overriding defaults USING expects certain variable types to appear in the parameter list depending on the contents of the formatting string. If the formatting string contains a decimal point then the default is a DOUBLE type. If no decimal point is specified then USING expects an INT variable type. To override the expected defaults use the %.. format specifier to inform USING what type of data you are including. USING will convert the data appropriately as needed to match the formatting string '%d is needed because there is no decimal point in the format string |