What is up with this in ColdFusion's DecimalFormat() function? How do I get the correct result?

前端 未结 4 1749
攒了一身酷
攒了一身酷 2021-01-18 16:27





DecimalFormat(#number1#): #DecimalFormat(n         


        
相关标签:
4条回答
  • 2021-01-18 17:09

    I think the problem is not DecimalFormat(), but the typical floating-point rounding errors.

    see: PrecisionEvaluate()

    0 讨论(0)
  • 2021-01-18 17:10

    A quick fix would be to change line 1 to number1 = NumberFormat(20.5/80 * 100,'9.999')

    0 讨论(0)
  • 2021-01-18 17:14

    DecimalFormat is a formatting function. Not a Mathematical function. Its job is not to round your number for you, unfortunately CF lacks good mathematical functions for decimals so you will have to write your own.

    Here is one someone wrote on the CF livedocs page for round():

     <cffunction name="roundDecimal" returntype="numeric"> 
         <cfargument name="num" type="numeric" required="yes"> 
         <cfargument name="decimal" type="numeric" default="2" required="no"> 
    
         <cfreturn round(num*10^decimal)/10^decimal /> 
      </cffunction>
    
    0 讨论(0)
  • 2021-01-18 17:22

    Please see the CF documentation NumberFormat and do a page search on round to see some specific information.

    0 讨论(0)
提交回复
热议问题