schibsted / jslt

JSON query and transformation language
Apache License 2.0
638 stars 120 forks source link

convert decimal numbers with trimmed zeros #367

Open paulheckmann opened 1 month ago

paulheckmann commented 1 month ago

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.

larsga commented 1 month ago

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.

catull commented 1 month ago

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 ?

paulheckmann commented 1 week ago

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?