Format function

  • Thread starter Thread starter KJ
  • Start date Start date
K

KJ

Hello,

Why does this not work?

txtMoney.Text.Format("#,##0.00")

If I type 100 in txtMoney, it returns #,##0.00

I have never gotten the format function to work in .NET.
 
Hi,


Dim strMoney As String = "12345.34"

Me.Text = CType(strMoney, Decimal).ToString("#,##0.00")

Ken
--------------------
________________________________________
From: KJ [mailto:[email protected]]
Sent: Tuesday, June 22, 2004 4:32 PM
To: microsoft.public.dotnet.languages.vb
Subject: Format function

Hello,

Why does this not work?

txtMoney.Text.Format("#,##0.00")

If I type 100 in txtMoney, it returns #,##0.00

I have never gotten the format function to work in .NET.
 
Hello,

Why does this not work?

txtMoney.Text.Format("#,##0.00")

If I type 100 in txtMoney, it returns #,##0.00

I have never gotten the format function to work in .NET.

Because the Format function is a member of the String class. txtMoney.Text
is a string and Format is a member of that class.

To use it, you should try:

txtMoney.Text = String.Format("#,##0.00", txtMoney.Text)


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
* (e-mail address removed) (KJ) scripsit:
Why does this not work?

txtMoney.Text.Format("#,##0.00")

If I type 100 in txtMoney, it returns #,##0.00

Try 'Strings.Format' instead.
 
The formatting constants are different in .NET - although I was under the
impression the classic VB ones worked too.
However, IMO the .NET formatting is better - have a look at ToString() in
the MSDN. The [decimal object].ToString(<format>) is what you're after.
Your code should be along the lines of

CType(txtMoney.Text, Decimal).ToString("C")

I'm sure the resident experts in this group can explain the details of the
formatting functions :D
___________________________________
The Grim Reaper
 
KJ,
As the others have suggested. txtMoney.Text.Format is really the shared
function String.Format, VB.NET allows you to called shared members on
instances of objects, as this example demostrates this can cause confused
results. To avoid these confused results I try to avoid calling shared
members on instances of objects (variables, properties, fields, parameters).

For details on using String.Format see:

http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconcompositeformatting.asp

http://msdn.microsoft.com/library/d...ef/html/frlrfSystemStringClassFormatTopic.asp


As Chris stated, you sample is the same as:

String.Format("#,##0.00")

There are two problems with this.
1. You are not using the value returned from the Format function.
2. You did not specify any placeholders in the format specifier.

Although the second is not important, the first surely is ;-)

Try something like:

Dim amount As Decimal

Dim str As String = String.Format("The total amount is {0:#,##0.00}",
amount)

Hope this helps
Jay
 
Hi Ken,

I see this is a dotnet.language.vb MVP contest.

The opinion from a supperdummy, I find yours the nicest.

Cor
 
* "Cor Ligthert said:
I see this is a dotnet.language.vb MVP contest.

The opinion from a supperdummy, I find yours the nicest.

I prefer 'CDec' in this situation ;-).
 
Hi Herfried,

I prefer 'CDec' in this situation ;-).

Almost the same question was in the adonet newsgroup, this was my answer on
the question.

\\\
Maybe you have seen it already it seems that there was a contest in the
language.vb newsgroup yesterday evening (my time) about this question.

A google link to this one and than

I found the one from Ken the nicest, however a simple answer is that you
have first to convert the string to a decimal before you can use the mask.
///
However I was no compatitor in the contest.

:-)

Cor
 
Just to let everyone know

THIS WORKED!!!!! :)
Me.Text = CType(strMoney, Decimal).ToString("#,##0.00")


THIS DID NOT WORK!!!! :(
txtMoney.Text = String.Format("#,##0.00", txtMoney.Text)


Thanks for your help!
 
Ken,
THIS DID NOT WORK!!!! :(
txtMoney.Text = String.Format("#,##0.00", txtMoney.Text)
Did you read my post, it explains why that one cannot work.

Hope this helps
Jay
 
Back
Top