我正在编写一些代码用于Xpath查询,这会导致问题.我正在尝试从列表中读取随机名称,但float(x)偶尔会返回指数表示法.
Random r = new Random();
float x = r.nextFloat();
final String expression = "(/list/name[@minLevel<=" + minLevel + "])" +
"[(" + x + " * count(/list/name[@minLevel<=" + minLevel + "])) +1]";
Run Code Online (Sandbox Code Playgroud)
每隔一段时间,表达式就等于:
(/name/list[@minLevel<=11])[(5.478859E-4 * count(/name/list[@minLevel<=11])) +1]
Run Code Online (Sandbox Code Playgroud)
显然xpath没有用指数表示法做数学,因为我得到这个错误:
javax.xml.transform.TransformerException: 5.478859E-4 could not be formatted to a number!
Run Code Online (Sandbox Code Playgroud)
任何有关如何防止这种情况的帮助将不胜感激.
要转换浮点数,可以使用String.format()
with %f
作为格式说明符.这绝不会使用科学记数法.
String.format("%f", x)
Run Code Online (Sandbox Code Playgroud)
这也使您可以根据需要灵活地调整宽度和精度.
归档时间: |
|
查看次数: |
1763 次 |
最近记录: |