Home > Enterprise >  Converting a string to a float in a Jenkins pipeline with standard security
Converting a string to a float in a Jenkins pipeline with standard security

Time:03-22

I'm hoping this is a duplicate as it seems like a basic thing to do, but I'm really struggling. I have a value that I need to check in a Jenkins declarative pipeline, for example this is a hardcoded equivalent;

String value = "7.5"

I want to fail the build if the value is above 7. I can do a similar thing with Integers by using a value.toInteger(), but as this is a decimal value it doesn't work here.

The closest I can get is;

throughputfloat = Float.parseFloat(value)

but I get;

Scripts not permitted to use staticMethod java.lang.Float parseFloat java.lang.String. Administrators can decide whether to approve or reject this signature.

Unfortunately our Admins won't approve this. Is there another way of converting a string to a float that doesn't need an admin's help?

CodePudding user response:

Never mind, just figured it out (I think). Instead of

def throughputfloat = Float.parseFloat(value)

It seems that;

Float throughputfloat = value

Is fine. I've no idea why but it seems to work.

CodePudding user response:

You have equivalent Groovy methods to type conversion analogous to the toInteger method for the String class. If toInteger is allowed in your sandbox security setting, then you also have available:

value.toBigDecimal()
value.toFloat()
value.toLong()

for three different options.

You also have even more options available with:

float newValue = value as Float
  • Related