Description
The NumberFormatGen Generator allows a referenced generated value to be formatted. You can use it to format numerical values to meet test data generation requirements such as:
- Padding numbers with leading zeros.
- Ensuring data is generated with the correct number of decimal places.
- Ensuring that the value has a fixed amount of digits.
- Ensuring commas are in the appropriate locations.
- Including a positive/negative sign.
For example, a user wants to take a number like 1234.09 and do the following:
- Show + or - sign
- Remove a decimal point
- Pad left up to 10 places
The generated result would be +0000123409.
In This Article
- Generator Parameters
- Example 1 - Pad with Five Zeros and Include Negative Sign
- Example 2 - Remove Decimal Point and Trailing Digits from Numbers
- Example 3 - Format Decimal Value to Contain Only Two Decimal Places
Generator Parameters
The following parameters may be configured for the NumberFormatGen Generator. Items with an asterisk* are required.
- reference* - Defines the Attribute to reference. IMPORTANT: The referenced Attribute should use a Generator that processes a floating point number. Example: #{com.acme.User.id}
- decimalFormat* - Defines the format for the decimal number in the generated result. Examples:
- Value|Pattern|Ouptut
- 123456.789|###,###.###|123,456.789
- 123456.789|###.##|123456.79
- 124.78|000000.000|000123.780
- 9.95|000.###|009.95
- 0.95|##0.###|0.95
- includeSign* - Determines whether to include the sign in the output value. The choices are NEVER, ALWAYS, POSITIVE_ONLY, NEGATIVE_ONLY.
- includeDecimal* - Determines whether to include the decimal in the output value.
- padLength* - If the value is greater than zero, it defines how many zeros should be padded to the left (e.g., if padLength = 10, then 120024 -> 0000120024).
Example 1 - Pad with Five Zeros and Include Negative Sign
For this example, two Generators will be linked and used to generate the output for an attribute:
- RangeDecimalGen (gen1) - Generates a decimal number that follows a pattern starting at -10 and increasing by +0.75 each iteration.
- NumberFormatGen (gen2) - References the RangeDecimalGen and pads the number up to five zeros while maintaining the values after the decimal point and its position.
Sample data is shown below:
RangeDecimalGen
The RangeDecimalGen has been assigned to generate decimal values containing two decimal places. The value starts at -10.00 and increases by 0.75 for each iteration.
NumberFormatGen
The NumberFormatGen will reference that generator and pad each value up to five zeros.
- reference - references the RangeDecimalGen (gen1) to obtain the decimal value.
- decimalFormat - 00000.00 to ensure the number is padded up to five zeros and not rounded or cut off.
- includeSign - NEGATIVE_ONLY to ensure the negative sign is included if the value is negative (e.g., -00010.75).
- includeDecimal - True
- padLength - 5
Example 2 - Remove Decimal Point and Trailing Digits from Numbers
A tester wants to remove the decimal point and trailing digits from a referenced value to make it a whole number. For this example, two generators will be assigned and linked in the attribute: RandomDecimalGen and NumberFormatGen.
- RandomDecimalGen (gen1) - generates a random decimal number.
- NumberFormatGen (gen2) - formats the decimal number by removing the decimal from each value.
The RandomDecimalGen generates a random decimal number and is referenced by the NumberFormatGen, which removes the decimal point and trailing digits from the number.
RandomDecimalGen
Generates a decimal number with four decimal places:
NumberFormatGen
This generator formats the decimal number by removing the decimal point and trailing digits.
- reference - references the RandomDecimalGen (gen1).
- includeDecimal - False
Example 3 - Format Decimal Value to Contain Only Two Decimal Places
A tester wants to ensure that each number only has two decimal places. For this example, two generators will be assigned and linked in the attribute: RandomDecimalGen and NumberFormatGen.
- RandomDecimalGen (gen1) - generates a random decimal number with four decimal places.
- NumberFormatGen (gen2) - formats the decimal number to two decimal places.
Sample data is shown below:
RandomDecimalGen
Generates a decimal number with four decimal places:
- decimalFormat - modified so that the number contains four decimal places.
NumberFormatGen
Format the number to have two decimal places.
- reference - references the RandomDecimalGen (gen1) for formatting.
- decimalFormat - formats the number to have two decimal places (###, ##0.00).