I have this String:
8.1161E-002
This string is actually a result of a calculation (unfortunately the result is converted to string and I can't change it).
So how could I convert this String to a format like 00.081
?
I'm looking at XPath reference but I can't find a format()
function for example. Is there a way of doing this in XPath?
If you are stuck with XPath 1.0 and your input is of the form (mantissa E+ exponent) or (mantissa E- exponent) you can use this hack (which is not very nice but gets the job done):
translate(
concat(
number(substring('100000000000000',1,number(substring-after(/number,'E+'))+1))*number(substring-before(/number,'E+')),
number(concat(substring('0.00000000000000',1,number(substring-after(/number,'E-'))+1),'1'))*number(substring-before(/number,'E-')),
number(substring('100000000000000',1,number(substring-after(/number,'e+'))+1))*number(substring-before(/number,'e+')),
number(concat(substring('0.00000000000000',1,number(substring-after(/number,'e-'))+1),'1'))*number(substring-before(/number,'e-')),
/number[not(contains(.,'E')) and not(contains(.,'e'))]
), 'Na', ''
)
It extracts the mantissa (string before the E
or e
) and then obtains a fraction or power of 10 by shifting a string of zeros the amount of positions determined by the exponent, Then it multiplies this with the mantissa. It deals separately with the case of positive or negative exponents. Since there is no if in XPath, it concatenates both results as strings, and allows one of them to produce 'NaN, which is removed later.
For example, if the exponent is +2
it will get the 100
substring from the first or third string. If it is +5
it will get 10000
and if it is -3
it will extract the 0.001
substring from the second or fourth strings. It then will multiply that number with the mantissa.
Since you will either have e+
or e-
, one of them will be the string NaN
which is then removed from the final string using the translate
function (it removes all N
and a
characters, which never occur in decimal numbers or scientific notation). The last concat
argument deals with the case where the number is not in scientific notation.
The e can be in upper or lower case.
Limitations:
+
a
will be removed from the final result).Applying it to this file:
<number>2.34</number>
it will read normally as:
2.34
But 2.34e+5
will be read as 234000
and 2.34E-005
will become 0.0000234
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With