开发者

Any way to change rounding method for LSCurrencyFormat in ColdFusion?

ColdFusion's LSCurrencyFormat function appears to 开发者_JAVA技巧be using round-to-half-even (banker's rounding). Is there anyway to change this? I'd like to change it to standard round-half-up rounding that most people are taught in grade school.

An example:

LSCurrencyFormat(39.7340): $39.73

LSCurrencyFormat(39.7350): $39.74

LSCurrencyFormat(39.7360): $39.74

LSCurrencyFormat(39.7440): $39.74

LSCurrencyFormat(39.7450): $39.74 <== I want this to be $39.75

LSCurrencyFormat(39.7460): $39.75


I do not think there is a way to customize the rounding mode used by the numeric functions. (Though I could be wrong) You may have to dip into java for customized rounding behavior

Update My mistake. I thought the need was for something basic masks did not already provide. Oh, well. Maybe this example will be useful to someone anyway ..

Update Added HALF_UP rounding mode example

(Note: The Locale handling is quick and dirty. I am sure there is a more elegant way of doing it..)

<cfset Locale = createObject("java", "java.util.Locale")>
<cfset Mode = createObject("java", "java.math.RoundingMode")>
<cfset Formatter = createObject("java", "java.text.NumberFormat").getCurrencyInstance(Locale.US)>
<cfset Formatter.applyPattern("$######,######.####")>
<cfset input = LSParseNumber("39.735", "en_US")>
Input <cfoutput>#input#<br></cfoutput>
<cfset Formatter.setRoundingMode( Mode.HALF_EVEN )>
HALF_EVEN <cfoutput>#Formatter.format(input)#<br></cfoutput>
<cfset Formatter.setRoundingMode( Mode.HALF_DOWN )>
HALF_DOWN <cfoutput>#Formatter.format(input)#<br></cfoutput>
<cfset Formatter.setRoundingMode( Mode.HALF_UP )>
HALF_UP <cfoutput>#Formatter.format(input)#<br></cfoutput>


Try LSNumberFormat() with a mask

#LSNumberFormat(39.7350,"$_.__")# = $39.7350


Why not round before you send the value to the LSCurrencyFormat?

LSCurrencyFormat(yourRoundingFunction(39.7450))


Also see http://jamiekrug.com/blog/index.cfm/2009/2/12/ColdFusion-round-function-bug-when-operation-performed-on-argument

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜