## Convert string in scientific notation to number format in XPath

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?

##### XPath 1.0 solution

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:**

- It
*won't*work if you have positive exponents*without*the`+`

- It will show incorrect results if the absolute value of the exponent is larger than 15.
- It also will fail if you have hexadecimal values (since any
`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`

.

**xpath exponent - Re: Floating point numbers in XPath,** Numbers are converted to strings using the following rules: * NaN is scientific notation for the string value of a number in this description. In scientific notation all numbers are written in the form of m×10 n (m times ten raised to the power of n), where the exponent n is an integer, and the coefficient m is any real number, called the significand or mantissa. If the number is negative then a minus sign precedes m (as in ordinary decimal notation).

You can use the function number(). This works with xpath 2.0.

Here is an example of how it would work.

XML

<this> <stuff>8.1161E-002</stuff> </this>

Xpath

/this/number(stuff)

Result

`0.081161`

Hope this helps!

**Working with Scientific Notation,** numbers represented in scientific notation to a standard decimal. The situation is this: I converted a spreadsheet from MS Excel 2002 into an MS XML XPath 2.0 will). My recommendation is to do this as a string processing operation, with as XPath 1.0 solution. 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

I've had the same issue on Xpath > 1.0. Just for completeness I solved mine with:

xs:decimal(sum(/a/b))

Works fine

**XSLT format-number() Function,** The format-number() function is used to convert a number into a string. Syntax. string format-number(number,format,[decimalformat]). Parameters. Parameter� Default Format: 12345.67890123 Scientific Notation: 1.23E+4 Default Format: 123456789 Scientific Notation: 1.23E+8 Default Format: 12345.67890123 Scientific Notation: 1.23E+04 turgay Posted in C# .NET , String Formatting Leave a comment

In case it helps, Sauer's solution has solved my problem. The line below was returning 3.00375e2. With xs:decimal now I get 300.375.

/products/xs:decimal(avg(product[contains(name/text(),"Microprocessor")]/price))

If you want to limit the number of decimals, you can use the round function, as follows:

/products/round(xs:decimal(avg(product[contains(name/text(),"Microprocessor")]/price))*100) div 100

**RE: [xsl] Outputting a number in "scientific notation" -- is this XSLT ,** XSLT 1.0 says that xsl:value-of converts the number to a string as if by the XPath does not use > scientific notation for floating point numbers,� gen str17 ids= string(id) but i get string that has scientific notation, for example 1.02e+09 or 3.03e+15. Why does this happen and how can i get string that just contains the long number, just in string type?

**XPath 2.0 Expression Syntax,** From Saxon 7.1, string delimiters can be doubled within the string to Numeric constants follow the Java rules for decimal literals: for example, 12 or 3.05; as a double precision floating point number if it uses scientific notation (e.g.) 1.0e7 ) According to the XPath 2.0 specification, numeric expression is converted to a� Converting numbers to string : The string() function converts a number to a string by the following ways: NaN (Not a Number) is converted to the string NaN. positive zero is converted to the string "0". negative zero is converted to the string "0". positive infinity is converted to the string "Infinity".

**xsl:decimal-format,** If the xsl:decimal-format element has a name attribute, it identifies a named to represent the xs:double value Nan (not-a-number); the default is the string ( NaN ). is new in XPath 3.1, and allows formating of numbers in scientific notation. When you convert real/float values to varchar/nvarchar, it will convert as the regualr decimal number when the digits are less than 7 otherwise it will use the scientific notation. So, here we are first parsing the string value into real and then parsing into money (money never uses the scientific notation).

**Convert a number in scientific notation to decimal format using XSLT ,** I have not find clear example on how to do this. I want to pass 2 regex-group result to variable inside analyse-string one should be tranformed from hexadecimal to� The representation of very small and very large numbers in the code above can be presented in default format or in a format the precludes use of scientific notation.

##### Comments

- In XPath 2.0 number() which will give you 0.081161 but in XPath 1.0 it will not be recognized as number and give you NaN. I don't think there is a ways of doing this with pure XPath 1.0. Where's that value comming from, can you fix it on that end?
- It comes from another system so I can't change it there. I tried number() and it does give me NaN as result. So there;s no way of solving this with pure XPath 1.0?
- @Dalek you can also use
`format-number()`

XSLT function. - No, I don't think there is a way... @alecxe in XSLT 1.0 format-number() would return NaN too.
- In an XSLT 1.0 context, this could be solved using the EXSLT extension function
`math:power`

together with`substring-before`

and`substring-after`

. - thanks for the answer. Question: How would this solution deal with "correct" inputs? For example when value = "10". It's tricky because I can't use an if statement in xpath 1.0.
- I fixed it. Now it deals with both scientific notation and "plain" numbers. I also made it simpler using
`translate()`

. - Omg, this is really crazy! You are an xpath genius :)