jslt icon indicating copy to clipboard operation
jslt copied to clipboard

convert decimal numbers with trimmed zeros

Open paulheckmann opened this issue 1 year ago • 2 comments

Currently the "number" function fails on this input: ".25" or "-.25".

Our data provider sends decimal numbers in that format, and so far JSLT is the only technology we use that cannot work with it.

We can work around it padding the string with a missing zero before feeding it into the "number" function.

paulheckmann avatar Sep 26 '24 09:09 paulheckmann

I think this is a fair point. The documentation of the number() function says it will convert the string into a number "if possible", and it definitely is possible to accept this number.

I checked, and the corresponding functions in Python, JavaScript, and Java all turn ".25" into 0.25. So clearly JSLT should do the same.

larsga avatar Sep 26 '24 09:09 larsga

With a trailing decimal point, the three mentioned languages have 2 different outcomes.

But then, JS internally only uses floating point numeric type, which "covers" integers.

JS

2 + 3. => 5

Python

2 + 3. => 5.0

Java

2 + 3. => 5.0 Only allowed for assignment to a double; illegal for an int or float

double a = 2 + 3

What do you want to support here ? Treat it as a dobule ?

catull avatar Sep 26 '24 14:09 catull

Sorry for the late response. In this case, I would expect that it treats it as a double. What would be alternative? That it fails on converting the int to a double?

paulheckmann avatar Nov 11 '24 08:11 paulheckmann

We treat ".25 and "0.25" as the same number, basically. All parsed numbers become DoubleNode in the Jackson representation. The initial zero does not affect the type.

Anyway, this has been fixed now. (Thanks @catull!) It would be great if those who care about this issue could do a little testing with the current code (@azpro). If everything looks OK I can release a new version.

larsga avatar Nov 28 '24 08:11 larsga