Formatting problem

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

I guess I still have not grasped the logic of formatting a simple label or
text box.

Why would the following not work? I need the resulting label to display
currency.

lblAmtDue.Text = CInt(txtEntries.Text) * 1.5

lblAmtDue.Text.Format("$#,##0.00")
 
Brad said:
I guess I still have not grasped the logic of formatting a simple label or
text box. .. . .
lblAmtDue.Text = CInt(txtEntries.Text) * 1.5

lblAmtDue.Text.Format("$#,##0.00")

This is what VB will effective do...

lblAmtDue.Text = ( CInt(txtEntries.Text) * 1.5 ).ToString()

Call lblAmtDue.Text.Format("$#,##0.00")

I think what you need is

lblAmtDue.Text = ( CInt( txtEntries.Text ) * 1.5 ).ToString("$#,##0.00")

and I'd /strongly/ recommend adding

Option Strict On

either as a Project [default] setting or at the top of the Module.

HTH,
Phill W.
 
Try the Format function...

lblAmtDue.Text = Format(CInt(txtEntries.Text) * 1.5, "$#,##0.00")

or better yet

use FormatCurrency function

Greg
 
Apply formatting on the integer itself.

lblAmtDue.Text = (CInt(txtEntries.Text) * 1.5).ToString("$#,##0.00")

I guess I still have not grasped the logic of formatting a simple label or
text box.

Why would the following not work? I need the resulting label to display
currency.

lblAmtDue.Text = CInt(txtEntries.Text) * 1.5

lblAmtDue.Text.Format("$#,##0.00")
 
Brad,
As the others have suggested, Text.Format is a function that returns a value
that is formatted, you are ignoring that value.

Rather then hard code the format to US dollars, I would use "C" a standard
format specifier for currency in region you are in.

I would recommend something like:

lblAmtDue.Text = (CInt(txtEntries.Text) * 1.5).ToString("C")

Which says take the integer value of txtEntries.Text, multiply that by 1.5,
convert the result of the multiplication to a string using the currency
format, then assign this formatted string to the Text property of lblAmtDue.

For details on Formatting in .NET see:

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

For standard numeric format strings (such as "C") see:

http://msdn.microsoft.com/library/d...de/html/cpconstandardnumericformatstrings.asp


I would also strongly recommend you include Option Strict On at the top of
your source files to avoid implicit conversions from Double to Text.

Hope this helps
Jay
 
Hi Brad,

Because Greg is all alone in this thread, I find this one real very nice

txtEntries.Text = FormatCurrency( CDec(txtEntries.Text)* 1.5)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top