Check decimals

Hello,
In my stream I turn JSON into a STRING.
I have numbers declared as an object that can therefore have values like below:
Null
178.0
178.25

And sometimes and by mistake:
0.181 instead of 181.0

I would like to test that the format of this number has at most only 2 decimals.
What is the invoke to use for this ?
This is my current flow in the attached file.

Thanks for your tips
2018-07-10_CheckDecimals.png

One option is to use a regex to check the format.

\d+(.\d{1,2})?

is one possible regex you could use. A search of the web will reveal other possibilities that may be more suited to your specific need.

That said, are you sure you want/need to do this “in the middle?” If the module is simply passing this value on to some system, why not let that system detect/enforce the format instead of doing it in middleware?

If you’re going to “fix” the value, be careful. Should you round? Truncate? Round up, down, half-up, etc.? People will forget that the middleware is explicitly modifying the data and you’ll end up with production support tickets about the data being “corrupted.”

Thanks for your answer.
But I don’t want to change the data (neither round nor truncate) but just test if I have more than 2 decimals to not process this record and just move on to the next.
The people in the trade make seizure errors that I want to intercept.
Without putting a test I had an error in the IS : “Error Logged. See Error log for details. Error: [SAP.102.9000] JCo error: com.sap.conn.jco.ConversionException - Number 0.181 cannot be encoded as a BCD of length 11 with 2 decimal places at field PRIX_OBJECTIF”
and the stream stopped.

Hope this helps, this is another way of doing without regex.

Say for example the number is 0.181 you can tokenize this number based on the delimiter (.) which will result in two parts

part 1 = 0
part 2= 181

Check the length of part 2 if greater than 2 then do not process this number and continue with other numbers.

For this, you can invoke string tokenize and branch on the valueList[1]>2

I checked the code and you are doing the same. Let me know if you have any questions.

Thanks but how to test the value of the list [2] > 2.
In my example, I calculate the length of the value [2] and I test in BRANCH /zLength if the length:
= 1, it’s OK
= 2, it’s OK
$default It’s Ko
Is there a better way ?

The optimized and best way to do this as below:

BRANCH (set evaluate labels=true)
%zLength%<=2 its OK
$default It’s KO

1 Like

Sorry, I’m a beginner on webmethods.
I have a mistake:
“Cannot define switch and use label conditions at ’ unlabeled BRANCH '”
I defined the BRANCH/zLenght with evaluate labels = True
and 2 sequences:

  1. Label:% zLength%< = 2 (exit on Failure)
  2. Label: $default
    Where do i put% zLength%< = 2?
    Thanks

Remove the zLength on branch and use only evaluate labels to true.

You have to check service development help guide and get more details. If you need any additional information please reach me on my email.

Thank you very much, it works as you suggested.

No worries, glad it worked for you.

So an exception is already being thrown – you do not need to test this yourself and throw a different exception. If you’re notifying someone of the error, use try/catch block to catch the ConversionException that is already being thrown. That way you’ll catch this and any other error that might happen.

“…optimized and best…” is debatable. :slight_smile: Another way, as noted earlier:

BRANCH on zTargetPrice
  /\d+(\.\d{1,2})?/: SEQUENCE (Ok)
  $default: SEQUENCE (Ko)

The regular expression in the branch will match if zTargetPrice is 1 or more numbers, optionally folllowed by a decimal and 1 or 2 numbers. No need for steps to tokenize nor get a length.

The way you have it in your original post, the way Mahesh provided and this way will all work. I still question the need to do it at all but without the additional info about what you’re doing when the error occurs or is detected by your steps, it’s hard to know for sure.