在ColdFusion中使用PrecisionEvaluate的科学记谱法 [英] Scientific Notation with PrecisionEvaluate in Coldfusion

查看:227
本文介绍了在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"

根据 Adob​​e文档,不推荐使用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的字符串表示
field ....

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天全站免登陆