9 Replies Latest reply on Sep 15, 2008 10:53 AM by Newsgroup_User

    Two Decimal Places

    Level 7
      what is the easiest way to round currency figures to two decimal places.

      I've googled and found conflicting advice I was quite surprised that
      coldfusion dosent have a simple function to perform this.

      All I want to do is change 4.475 for example to 4.48

      any help appreciated

      Brett


        • 1. Re: Two Decimal Places
          Level 7
          How about NumberFormat?

          http://livedocs.adobe.com/coldfusion/7/htmldocs/00000591.htm#134113

          <cfset myNumber = 4.475>
          <cfoutput>#NumberFormat(myNumber, "999,999.99")#</cfoutput>


          --
          Ken Ford
          Adobe Community Expert
          Fordwebs, LLC
          http://www.fordwebs.com


          "Sammy" <orders@seton.net.au> wrote in message news:f42ulc$huo$1@forums.macromedia.com...
          > what is the easiest way to round currency figures to two decimal places.
          >
          > I've googled and found conflicting advice I was quite surprised that
          > coldfusion dosent have a simple function to perform this.
          >
          > All I want to do is change 4.475 for example to 4.48
          >
          > any help appreciated
          >
          > Brett
          >
          >
          • 2. Re: Two Decimal Places
            jdeline Level 1
            Good answer, Ken. I notice that #Round(4.475 * 100) / 100# doesn't return 4.48, it returns 4.47. I would expect it to round up.
            • 3. Re: Two Decimal Places
              Level 7
              Or maybe the decimalFormat() function?
              • 4. Re: Two Decimal Places
                Level 7
                > I've googled and found conflicting advice I was quite surprised that
                > coldfusion dosent have a simple function to perform this.

                Did you try *looking in the docs*?

                I'm surprised you didn't find one of these:
                http://livedocs.adobe.com/coldfusion/7/htmldocs/00000450.htm
                http://livedocs.adobe.com/coldfusion/7/htmldocs/00000591.htm

                Or, if you're doing this for currency:
                http://livedocs.adobe.com/coldfusion/7/htmldocs/00000455.htm
                http://livedocs.adobe.com/coldfusion/7/htmldocs/00000570.htm

                --
                Adam
                • 5. Re: Two Decimal Places
                  commtogether
                  In Actionscript I use:-

                  var denom = denom;
                  var nom = nom;
                  var temp = temp;

                  temp = int(Net*100)/100;
                  temp = Math.round(temp*100);
                  nom = Math.floor(temp/100);
                  denom = Math.round((temp/100-nom)*100);
                  var zeros:String = "000000000"+denom.toString();
                  denom = zeros.substr(-2,2);
                  Net = nom+'.'+denom;

                  where 'Net' is the raw 9876543.12345678 number

                  --Andy
                  • 6. Re: Two Decimal Places
                    Level 7
                    > Good answer, Ken. I notice that #Round(4.475 * 100) / 100# doesn't return 4.48, it returns 4.47. I would expect it to round up.

                    Interesting.

                    Check this out:

                    <cfscript>
                    f1 = 4.475;
                    writeOutput("f1 = 4.475: [#f1#]<br />");
                    f2 = f1 * 100;
                    writeOutput("f2 = #f1# * 100: [#f2#]<br />");
                    f3 = round(f2);
                    writeOutput("f3 = round(#f2#): [#f3#]*** wrong<br />");
                    f4 = f3 / 100;
                    writeOutput("f4 = #f3# / 100: [#f4#]<br />");

                    writeOutput("<hr />");

                    f5 = round(447.5);
                    writeOutput("round(447.5): [#f5#]*** right<br />");
                    </cfscript>

                    I'm presuming it's some under-the-hood floating point rounding error on the
                    f2 operation which means it's actually 4.4749999999. Or something like
                    that.

                    It's still a bug, though, I reckon.

                    --
                    Adam
                    • 7. Re: Two Decimal Places
                      BKBK Adobe Community Professional & MVP
                      > easiest way to round currency figures to two decimal places

                      <cfoutput># LScurrencyformat(4.475,"local")#</cfoutput>

                      • 8. Re: Two Decimal Places
                        obouillaud Level 1
                        I think there is a bug on the rounding method of LSCurrencyformat and (in my case) LSEurocurrencyFormat

                        Here is a simple code :
                        <cfoutput>
                        55.905 - #LScurrencyformat(55.905, "local")#<br />
                        55.915 - #LScurrencyformat(55.915, "local")#<br />
                        55.925 - #LScurrencyformat(55.925, "local")#<br />
                        55.935 - #LScurrencyformat(55.935, "local")#<br />
                        55.945 - #LScurrencyformat(55.945, "local")#<br />
                        55.955 - #LScurrencyformat(55.955, "local")#<br />
                        55.965 - #LScurrencyformat(55.965, "local")#<br />
                        55.975 - #LScurrencyformat(55.975, "local")#<br />
                        55.985 - #LScurrencyformat(55.985, "local")#<br />
                        55.995 - #LScurrencyformat(55.995, "local")#<br />
                        55.005 - #LScurrencyformat(56.005, "local")#<br />
                        </cfoutput>

                        On my CF8 server the result is :

                        55.905 - 55,90 €
                        55.915 - 55,92 €
                        55.925 - 55,92 €
                        55.935 - 55,94 €
                        55.945 - 55,94 €
                        55.955 - 55,96 €
                        55.965 - 55,96 €
                        55.975 - 55,98 €
                        55.985 - 55,98 €
                        55.995 - 56,00 €
                        55.005 - 56,00 €

                        I tried with <cfset setlocale("english (us)")> or other values, it does not have any impact.

                        Can you confirm that you have the same behavior ?
                        • 9. Re: Two Decimal Places
                          Level 7
                          obouillaud wrote:
                          > I think there is a bug on the rounding method of LSCurrencyformat and (in my
                          > case) LSEurocurrencyFormat

                          i just tested w/core java using:

                          <cfscript>
                          numbers=listToArray("55.905,55.915,55.925,55.935,55.945,55.955,55.965,55.975,55.985,55.995 ,55.005");
                          locale=createObject("java","java.util.Locale").init("fr","FR");
                          nf=createObject("java","java.text.NumberFormat").getCurrencyInstance(locale);
                          for (i=1; i <= arrayLen(numbers); i++) {
                          writeoutput("#numbers #-#nf.Format(javaCast("double",numbers))# <br>");
                          }
                          </cfscript>

                          which produced

                          55.905 - 55,90 €
                          55.915 - 55,92 €
                          55.925 - 55,92 €
                          55.935 - 55,94 €
                          55.945 - 55,94 €
                          55.955 - 55,96 €
                          55.965 - 55,96 €
                          55.975 - 55,98 €
                          55.985 - 55,98 €
                          55.995 - 56,00 €
                          55.005 - 55,00 €

                          so, yup, looks like a bug w/that method. dollarFormat doesn't seem to show this
                          behavior w/your numbers. icu4j's numberFormat class (using currency &
                          currencyAmount classes) work fine as well.

                          i'll bring to the cf's team attention.