Skip to main content
January 9, 2009
Question

dollarFormat / numberformat problem

  • January 9, 2009
  • 12 replies
  • 3264 views
There has to be a super simple answer to this, I just cant find it.

In my classifieds when people enter a number, some will enter 1000, some will enter 1,000, some will enter 1,000.00

now when I try to DollarFormat 1000 works but the rest dont. I've tried different varaitions, using numberFormat, find and replace comma...everything just gives me more trouble.. here is the closest I've gotten.

<cfform action="test.cfm" method="post"><cfinput name="price" type="text" validate="float" align="right" /><cfinput name="submit" type="submit" /></cfform>
<br />
<br />
<cfif isDefined("submit")>
</cfif>
<hr />
<cfoutput>
<cfif listlen(price,",") GT 1>
<cfset newstr = rereplace(#form.price#, "[^0-9]+", "", "All")>
<cfset newerstr = (newstr / 100) >
PRICE #form.price#<br />
NEWRSTR #newerstr#<br />
a. #numberFormat(newerstr, '999,999,999.99')#
<cfelse>

b. #numberFormat(price, '999,999,999.99')#</cfif>
</cfoutput>


any help would be appriciated

C.
This topic has been closed for replies.

12 replies

BKBK
Community Expert
Community Expert
January 13, 2009
you mean that in a locale where 12345,00 is a valid number...

In no locale is 12345,00 a valid Coldfusion number.



Inspiring
January 12, 2009
BKBK wrote:
> It is supposed to work for all locales. DollarFormat(x) is irrespective
> of locale. The docs say it "Formats a string in U.S. format. Returns a

and before that the docs say: "Formats a string in U.S. format. (For other
currencies, use LSCurrencyFormat or LSEuroCurrencyFormat."

it's not designed/intended for use outside en_US. that's what the LS functions
are for.
BKBK
Community Expert
Community Expert
January 12, 2009
BKBK wrote:
> It is supposed to work for all locales. DollarFormat(x) is irrespective
> of locale. The docs say it "Formats a string in U.S. format.

PaulH wrote:
and before that the docs say: "Formats a string in U.S. format. (For other
currencies, use LSCurrencyFormat or LSEuroCurrencyFormat."

it's not designed/intended for use outside en_US. that's what the LS functions
are for.


Not true. This is just a matter of logic. The fact that LSCurrencyFormat or LSEuroCurrencyFormat are suited to non-U.S. locales does not mean dollarFormat is unsuitable for those locales. I'll mention it again: dollarFormat is a function to convert a Coldfusion number to U.S. dollar format in every locale. It takes just one parameter, a number. Not by accident. It is a universal, locale-independent function.


BKBK
Community Expert
Community Expert
January 11, 2009
Fober1 wrote:
The challenge here is to interpret what the user meant with the data entered in the edit field:
A US user may enter "1,234.56", but a user in EMEA may enter the same number in this format "1.234,56".
If you just remove the comma, the number will not get interpreted correctly.


I went by what the original poster, Izdabye, said. "In my classifieds when people enter a number, some will enter 1000, some will enter 1,000, some will enter 1,000.00"

The documentation says Coldfusion expects the argument x in dollarFormat(x) to have the value of a Coldfusion number. You may put x=1.23456 if you like. Then you'll get the dollar you deserve.



BKBK
Community Expert
Community Expert
January 11, 2009
BKBK wrote:
>> DollarFormat works for all locales!

PaulH wrote:
> it's not supposed to.

It is supposed to work for all locales. DollarFormat(x) is irrespective of locale. The docs say it "Formats a string in U.S. format. Returns a number as a string, formatted with two decimal places, thousands separator, and dollar sign. If number is negative, the return value is enclosed in parentheses. If number is an empty string, returns zero."


Inspiring
January 11, 2009
BKBK wrote:
> DollarFormat works for all locales!

it's not supposed to.
Inspiring
January 10, 2009
> In my classifieds when people enter a number, some will enter 1000, some will
> enter 1,000, some will enter 1,000.00
>
> now when I try to DollarFormat 1000 works but the rest dont.

What do you mean when you suggest "it doesn't work"? What's happening? Is
it erroring? Are you getting results you don't expect (if so, what are
they?)

--
Adam
Inspiring
January 9, 2009
DollarFormat() is an output function.
The challenge here is to interpret what the user meant with the data entered in the edit field:
A US user may enter "1,234.56", but a user in EMEA may enter the same number in this format "1.234,56".
If you just remove the comma, the number will not get interpreted correctly.



BKBK
Community Expert
Community Expert
January 9, 2009
@PaulH
uh, just make sure the user's locale doesn't swap the meaning of commas & periods it's locale number formatting (or uses spaces).

DollarFormat works for all locales!



Inspiring
January 9, 2009
Hi,

Any reason for not using LSParseNumber()?

cheers,
fober


<cfparam name="price" default="">

<form method="post">
<input name="price" type="text" align="right" />
<input name="submit" type="submit" />
</form>

<br />
<br />
<cfif isDefined("submit")>
</cfif>
<hr />

<cfif LSIsNumeric(price)>
<cfset newprice= LSParseNumber(price)>
<cfelse>
<cfset newprice= 0>
</cfif>


<cfoutput>
before: #price#<br>
after: #newprice#<br>
</cfoutput>

Inspiring
January 9, 2009
BKBK wrote:
> You've suggested the most obvious answer yourself. Just delete the commas
> before doing the number format. Something like this should do it:

uh, just make sure the user's locale doesn't swap the meaning of commas &
periods it's locale number formatting (or uses spaces).