• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Two Decimal Places

LEGEND ,
Jun 04, 2007 Jun 04, 2007

Copy link to clipboard

Copied

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


TOPICS
Advanced techniques

Views

1.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

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
>
>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

Or maybe the decimalFormat() function?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

> 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2007 Jun 05, 2007

Copy link to clipboard

Copied

> easiest way to round currency figures to two decimal places

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2008 Sep 15, 2008

Copy link to clipboard

Copied

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 ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2008 Sep 15, 2008

Copy link to clipboard

Copied

LATEST
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation