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

dollarFormat / numberformat problem

Guest
Jan 08, 2009 Jan 08, 2009
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.
TOPICS
Getting started
3.2K
Translate
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
Advocate ,
Jan 08, 2009 Jan 08, 2009
Hi izdabye,

Try the "DollarFormat2" UDF,

You may download it here,

http://cflib.org/udf/DollarFormat2

HTH
Translate
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 ,
Jan 08, 2009 Jan 08, 2009
You've suggested the most obvious answer yourself. Just delete the commas before doing the number format. Something like this should do it:

<cfset amount = "1,000,000.07">
<cfset amount = replace(amount, ",", "", "all")>
<cfset amountInDollars = dollarFormat(amount)>
<cfoutput>#amountInDollars#</cfoutput>

Translate
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 ,
Jan 08, 2009 Jan 08, 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).
Translate
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
Participant ,
Jan 09, 2009 Jan 09, 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>

Translate
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 ,
Jan 09, 2009 Jan 09, 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!



Translate
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
Participant ,
Jan 09, 2009 Jan 09, 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.



Translate
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 ,
Jan 10, 2009 Jan 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
Translate
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 ,
Jan 10, 2009 Jan 10, 2009
BKBK wrote:
> DollarFormat works for all locales!

it's not supposed to.
Translate
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 ,
Jan 11, 2009 Jan 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."


Translate
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 ,
Jan 11, 2009 Jan 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.



Translate
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 ,
Jan 11, 2009 Jan 11, 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.
Translate
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 ,
Jan 12, 2009 Jan 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.


Translate
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 ,
Jan 12, 2009 Jan 12, 2009
> 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.

This is beginning to sound like an allegory for current US foreign policy.

Ahem.

On a lighter note, it also reminds me of a time I was in a shop - in London
- and witnessed a plaid-wearing US tourist become at first bemused, then
indignant that their US money wasn't an acceptable form of currency in the
UK, saying to the shopkeeper - somewhat condescendingly - "but these are
*US dollars*!!".

But anyway.

I suppose it's great we all have a function which will present a number
formatted as US currency, irrespective of where we are in the world.

--
Adam
Translate
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 ,
Jan 12, 2009 Jan 12, 2009
BKBK wrote:
> 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

you mean that in a locale where 12345,00 is a valid number will work
w/dollarFormat()? no, i don't think so.
Translate
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 ,
Jan 13, 2009 Jan 13, 2009
LATEST
you mean that in a locale where 12345,00 is a valid number...

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



Translate
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