将科学记数法中的字符串转换为 XPath 中的数字格式 [英] Convert string in scientific notation to number format in XPath

查看:34
本文介绍了将科学记数法中的字符串转换为 XPath 中的数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串:

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).

那么我怎么能把这个字符串转换成00.081这样的格式?

So how could I convert this String to a format like 00.081?

我正在查看 XPath 参考,但我找不到例如 format() 函数.有没有办法在 XPath 中做到这一点?

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 解决方案

如果您坚持使用 XPath 1.0 并且您的输入格式为 (尾数 E+ 指数)或者(尾数 E- 指数)你可以使用这个hack(这不是很好,但可以胜任完成):

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', ''
)

它提取尾数(Ee 之前的字符串),然后通过移动一串将指数确定的位置的数量归零,然后将其与尾数相乘.它分别处理正指数或负指数的情况.由于 XPath 中没有 if,它将两个结果连接为字符串,并允许其中之一产生 'NaN,稍后将其删除.

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.

例如,如果 exponent+2 它将从第一个或第三个字符串中获取 100 子字符串.如果是 +5 它将得到 10000 并且如果它是 -3 它将从中提取 0.001 子字符串第二个或第四个字符串.然后它将该数字乘以尾数.

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.

由于您将拥有 e+e-,其中之一将是字符串 NaN,然后将其从最终字符串中删除使用 translate 函数(它删除所有 Na 字符,这些字符永远不会出现在十进制数或科学记数法中).最后一个 concat 参数处理科学计数法中的数字不是的情况.

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.

e 可以是大写或小写.

限制:

  1. 如果你有正指数没有+
  2. ,它不会工作
  3. 如果指数的绝对值大于 15,它会显示错误的结果.
  4. 如果您有十六进制值,它也会失败(因为任何 a 都将从最终结果中删除).
  1. It won't work if you have positive exponents without the +
  2. It will show incorrect results if the absolute value of the exponent is larger than 15.
  3. It also will fail if you have hexadecimal values (since any a will be removed from the final result).

将其应用于此文件:

<number>2.34</number>

它会正常读取为:

2.34

但是2.34e+5 会读成2340002.34E-005 会变成0.0000234.

But 2.34e+5 will be read as 234000 and 2.34E-005 will become 0.0000234.

这篇关于将科学记数法中的字符串转换为 XPath 中的数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆