Coldfusion 中的 PrecisionEvaluate 科学记数法 [英] Scientific Notation with PrecisionEvaluate in Coldfusion

查看:29
本文介绍了Coldfusion 中的 PrecisionEvaluate 科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理大数和长十进制数时遇到问题,正如其他人使用 PrecisionEvaluate 提到或解决了此类问题一样,我无法使用此类功能获得一致的结果.

I have problems working with large numbers and long decimal numbers, as others have mentioned or solved such issue using PrecisionEvaluate, I could not get consistent result with such function.

此代码示例:

<cfset n = 0.000000000009>
<cfoutput>#precisionEvaluate(n)#</cfoutput> // this will produce "9E-12"
<cfoutput>#precisionEvaluate("n")#</cfoutput> // this will produce "0.000000000009"

根据 Adobe 文档,不建议使用 Quote(由于处理效率低下)以及它应该产生相同的结果,但是从上面的代码中不是这种情况.

According to Adobe Documentation, using Quote is not recommended (due to processing inefficiency) as well as it should produce same result, however this is not the case from the above code.

结果不一致的进一步试验:

Further trials with inconsistent result:

<cfset n = 0.000000000009>
<cfset r = 12567.8903>
<cfoutput>#precisionEvaluate(r * n)#</cfoutput> // this will produce "1.131110127E-7"
<cfoutput>#precisionEvaluate("r * n")#</cfoutput> // this will produce "1.131110127E-7", same as above
<cfoutput>#precisionEvaluate(r / n)#</cfoutput> // this will produce "1396432255555555.55555555555555555556"
<cfoutput>#precisionEvaluate("r / n")#</cfoutput> // this will produce "1396432255555555.55555555555555555556", same as above

有没有人遇到过类似案例的问题?解决不一致的实际解决方案是什么?我试过:使用 val() 函数无法解决,因为它仅限于短数字,使用 numberFormat() 函数,这很困难,因为我们必须传递小数位数才能正确格式化.

Has anybody run into problems with a similar case? What is a practical solution to address the inconsistency? I have tried : using val() function does not resolve as it is limited to short numbers only, using numberFormat() function which is difficult as we have to pass number of decimals to format it properly.

推荐答案

当谈到数字时,不要总是相信你在屏幕上看到的东西.这只是数字的人类友好"表示.在您的情况下,实际结果(或数字) 是一致的.这只是如何显示这些数字的问题..

When it comes to numbers, do not always believe what you see on the screen. That is just a "human friendly" representation of the number. In your case, the actual results (or numbers) are consistent. It is just a matter of how those numbers are presented ..

PrecisionEvaluate 返回一个 java.math.BigDecimal 对象.为了在 <cfoutput> 中显示该对象表示的数字,CF 调用对象的 toString() 方法.根据 API,toString() 可以使用科学记数法来表示值.这就解释了为什么它用于您的某些值,而不是其他值.(尽管有或没有指数,它仍然代表相同的数字).但是,如果您希望排除指数,只需使用 BigDecimal.toPlainString() 改为:

PrecisionEvaluate returns a java.math.BigDecimal object. In order to display the number represented by that object inside <cfoutput>, CF invokes the object's toString() method. Per the API, toString() may use scientific notation to represent the value. That explains why it is used for some of your values, but not others. (Though with or without the exponent, it still represents the same number). However, if you prefer to exclude the exponent, just use BigDecimal.toPlainString() instead:

toPlainString() - 返回此 BigDecimal 的字符串表示,不带指数场……

toPlainString() - Returns a string representation of this BigDecimal without an exponent field....

示例:

<cfscript>
    n = 0.000000000009;
    r = 12567.8903;
    result = precisionEvaluate(r * n);

    WriteOutput( result.getClass().name );
    WriteOutput("<br />result.toString() ="&  result.toString());
    WriteOutput("<br />result.toPlainString() ="&  result.toPlainString());
</cfscript>

结果:

java.math.BigDecimal
result.toString() =1.131110127E-7
result.toPlainString() =0.0000001131110127 

这篇关于Coldfusion 中的 PrecisionEvaluate 科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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