Fighting with MAP formatting

I don’t work with Map variable types very often, however I need to in one script type that I’m running and I can’t get the output quite right (Yes, I’m OCD, sigh).

Essentially I’m declaring the Map type as <String, Integer>:

Map <String, Integer> console_values = ["Onload":0,"FCP":0,"LCP":0,"FirstByte":0,"DNS":0,"TCP":0,"DOM":0,"SSL":0,"Redirect":0,"BasePage":0,"CLS":0]

Populating using Math.round to convert any decimal values into integers

example:

/// Base Page
String jsBP = "var myCalc= Math.round((window.performance.timing.responseEnd - window.performance.timing.responseStart)); return myCalc;"

console_values ["BasePage"]  = execute_js_in_console(jsBP, 'basePage')

and using a simple logfile output (for now):

		KeywordUtil.logInfo ('Onload: ' + console_values["Onload"])
		KeywordUtil.logInfo ('FCP: ' + console_values["FCP"])
		KeywordUtil.logInfo ('LCP: ' + console_values["LCP"])
		KeywordUtil.logInfo ('FirstByte: ' + console_values["FirstByte"])
		KeywordUtil.logInfo ('DNS: ' + console_values["DNS"])
		KeywordUtil.logInfo ('TCP: ' + console_values["TCP"])
		KeywordUtil.logInfo ('DOM: ' + console_values["DOM"])
		KeywordUtil.logInfo ('SSL: ' + console_values["SSL"])
		KeywordUtil.logInfo ('Redirect: ' + console_values["Redirect"])
		KeywordUtil.logInfo ('BasePage: ' + console_values["BasePage"])
		KeywordUtil.logInfo ('CLS: ' + console_values["CLS"])

However the output is showing as decimal:

image

How the heck is an Integer defined value continually outputting as a decimal type?

I feel like there’s something simple I’ve got to be missing here, yet I’ve spent several hours looking at the Java MAP API and community post after community post and I just can’t get rid of the dang “decimal zero”, what am I doing incorrectly?

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

PLS try this:

import java.text.DecimalFormat

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser("https://www.google.com")
def v = WebUI.executeJavaScript("return 1.2345", null)
WebUI.closeBrowser()

println "v is instance of " + v.getClass().getName()
println "v=" + v

DecimalFormat df1 = new DecimalFormat("#,###,###,##0.00");
DecimalFormat df2 = new DecimalFormat("#,###,###,##0");

println "v=" + df1.format(v)
println "v=" + df2.format(v)

this will emit

v is instance of java.lang.Double
v=1.2345
v=1.23
v=1

1 Like

Just for your information, see the following script

import java.text.DecimalFormat

Map<String, Integer> m = ["OnLoad":0]
println "${m.get("OnLoad")}, ${m.get("OnLoad").getClass().getName()}"
m.put("OnLoad", 1.2345)
println "${m.get("OnLoad")}, ${m.get("OnLoad").getClass().getName()}"
String s = new DecimalFormat("#,###,###,##0").format(m.get("OnLoad"));
println "${s}, ${s.getClass().getName()}"

and the output from it.

2024-06-05 19:50:51.587 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2024-06-05 19:50:51.592 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/MapType
0, java.lang.Integer
1.2345, java.math.BigDecimal
1, java.lang.String
2024-06-05 19:50:52.629 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/MapType

This example tells you that Groovy language is different from Java as far as handing the numeric data types. Groovy is not strict about data types as Java. Groovy converts numeric data types implicity as needed. Then, how Groovy manages conversions of numeric data types? ---- well, I don’t know it. I don’t need to care.

All you need to learn is how to format a numeric value into a String as you like (with or without decimal points).

First example worked perfectly!

Thanks for the solution, blasted Groovy :slight_smile: