Currency Formatting with getCurrencyInstance()
One of my normal practices is to strip all the formatting from a site value, simulate the site calculations, then compare my calculated value to the site value and display the results in the log file. But without formatting, the results can be hard to read as they go scrolling by. To add the formatting back is a relatively simply process. We can use the NumberFormat package and the format option to put the $ and , where they belong. Thus, 16384 becomes $16,384.
To use the formatting, import the following libraries, which sets the formatting based on locale.
import java.text.NumberFormat import java.util.Locale
Next, create reference to the NumberFormat library:
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance()
Finally, format the number back to a dollar figure:
log.logWarning(‘Total amount in dollars is:’ + defaultFormat.format(totalAmount))
One thing to note, the value to format must be an integer, float, double or big decimal value. Basically, anything other than a string.
As a basic example:
import com.kms.katalon.core.logging.KeywordLogger as KeywordLogger
import java.text.NumberFormat
import java.util.Locale
KeywordLogger log = new KeywordLogger()
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance()
double dollarFigure = 16384.526;
log.logWarning(“The dollar figure is: ” + defaultFormat.format(dollarFigure))
Output:
The dollar figure is: $16,384.53
Other articles of interest:
- Output status messages and test information by writing to the Log File Viewer in Katalon Studio
- New Libraries to Import into Katalon Studio
- Stripping away characters with ReplaceAll in Katalon Studio
- Working with Dates and Date Formatting in Katalon Studio
- Data Driven API Testing with Katalon using Spreadsheet Data
- Reviewing the Execution Logs of Katalon Studio
- Working with Tables using the Selenium WebDriver and Katalon Studio
- Marking tests as Passed, Failed or in Error using MarkFailed, MarkPassed, MarkError in Katalon Studio
- Setting up a repeatable Search Method in Katalon Studio
- Creating and Calling Methods for Test Cases in Katalon Studio
Leave a Reply