I would like to ask if there's a way to parse a version number using a groovy script.
I extract from Ariba a payload, the issue comes with a specific field called ItemNumber
.
At first it was working, but this month I started to retrieve a version instead of a float.
This is the part of the script that needs to be changed, but I can't find a way to do it.
if (ItemNumber?.trim()){
list.ItemNumber = Double.parseDouble(ItemNumber.toString());
}
EDIT: This is the field I retrieve: { "ItemNumber": "4.4.5" }
.
I would like to get this: { "ItemNumber" : 4.4.5 }
.
Any help is greatly appreciated,
Thank you, Kostas
To parse a version number, you will need to tokenize the string. You should create a
Version
class to hold the version information. This can help when sorting a list of versions. After tokenizing, you can call theVersion
constructor, and pass the tokens as integers.Once you have a
Version
object, you can access fields likemajor
,minor
, andpatch
.In later versions of Groovy, you can call the
Versions
constructor like this: