Monday, March 26, 2012

Trouble formating string for label.

Hey everyone!

Weird situation here. I have this label:

<asp:label id="lblDepositDue" runat="server" />

I am trying to set it's value with this statement:
lblDepositDue.Text = "Deposit Due Upon Booking: " & String.Format("{0:c}", intDepositDue.ToString)

Simple right? But the output still comes out looking like this:

Deposit Due Upon Booking: 4999

...instead of like this which SHOULD be the output:

Deposit Due Upon Booking: $4,999.00

What is going wrong?

Thanks.....MikeHow about something like this:


lblProductCost.Text = Format(intProductCost, "#,##0.00")

Hey...

When I try:

lblDepositDue.Text = "Deposit Due Upon Booking: " & Format(intTotalTourCost.ToString, "#,##0.00")

It comes out like this:

Deposit Due Upon Booking: #,##0.00

:-/
Hi,

as was said, do not call ToString for the integer argument passed to the Format function (it was same problem with your original code):

Instead of:


Format(intTotalTourCost.ToString, "#,##0.00")

use

Format(intTotalTourCost, "#,##0.00")

In your original code following would have worked:


String.Format("{0:c}",intDepositDue)

Note that ToString is not called for the argument. String formatting is formatting from another data type type to string. Capability to format comes when argument is typed, If you pass it as string with ToString method, it's original type is not known (strictly speaking is, it is string as well and goes through formatting as is) and it can't be formatted.
Teemu,

Thank you very much for the clarification. I was under the (incorrect) assumption that String.Format required that the value passed to is was in string format already. I wasn't aware of any conversion to string simply by formatting.

Thanks again!

Mike

0 comments:

Post a Comment