views:

52

answers:

4

ColdFusion's LSCurrencyFormat function appears to 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

+1  A: 

Try LSNumberFormat() with a mask

#LSNumberFormat(39.7350,"$_.__")# = $39.7350
Pragnesh Vaghela
+3  A: 

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>
Leigh
A: 

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

LSCurrencyFormat(yourRoundingFunction(39.7450))
Jean Moniatte
Well the hope was that I could do it in a locale-specific way. If I'm in Japan, I don't want anything after the decimal (since Yen are usually integers)
Kip