469,890 Members | 2,058 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Format function

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.
Nov 20 '05 #1
10 8011
Hi,
Dim strMoney As String = "12345.34"

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

Ken
--------------------
________________________________________
From: KJ [mailto:kl******@hotmail.com]
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.

--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.3 - Release Date: 6/18/2004
Nov 20 '05 #2
On 22 Jun 2004 13:32:00 -0700, KJ wrote:
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.
Nov 20 '05 #3
* kl******@hotmail.com (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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
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" <kl******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
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.

Nov 20 '05 #5
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/de...formatting.asp

http://msdn.microsoft.com/library/de...ormatTopic.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

"KJ" <kl******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
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.

Nov 20 '05 #6
Hi Ken,

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

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

Cor
Dim strMoney As String = "12345.34"

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

Nov 20 '05 #7
* "Cor Ligthert" <no**********@planet.nl> scripsit:
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 ;-).
Me.Text = CType(strMoney, Decimal).ToString("#,##0.00")


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8
Hi Herfried,

I prefer 'CDec' in this situation ;-).
Me.Text = CType(strMoney, Decimal).ToString("#,##0.00")


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
Nov 20 '05 #9
KJ
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!
Nov 20 '05 #10
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

"KJ" <kl******@hotmail.com> wrote in message
news:d8**************************@posting.google.c om... 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!

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jean | last post: by
6 posts views Thread by Stuart McGraw | last post: by
13 posts views Thread by Roy Hills | last post: by
4 posts views Thread by Matt | last post: by
16 posts views Thread by Al Reid | last post: by
4 posts views Thread by sql guy123 | last post: by
11 posts views Thread by shsandeep | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.