Format Number as in ToString("#,###.00")

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hello Folks,

I am bring backa data reader dr from the database/

So I have a number of fields such as dr["ExtendedPrice"]

To put this in a table I use "<td>" + dr["ExtendedPrice"].ToString()
+ "</td>"

But I want to format the number as mentioned in the subject line.

It won't work...tells me that ToString accepts 1 argument.

What am I doing wrong?

Thanks-in-Advance,

Bob Sweeney
 
What am I doing wrong?

Not converting the object from the DataReader into something which can have
text formatting applied to it...

"<td>" + Convert.ToDecimal(dr["ExtendedPrice"]).ToString("#,##0.00") +
"</td>"
 
At the time, you only have an object; to use this overload you either
need to know what the datatype is (int, decimal, float, double, etc -
I'm guessing double, so ((double)dr["ExtendedPrice"]).ToString(...)),
or (if you can't predict the type, but think it should work) use the
IFormattable interface (i.e.
((IFormattable)dr["ExtendedPrice"]).ToString(...))

Marc
 
Hello Folks,

I am bring backa data reader dr from the database/

So I have a number of fields such as dr["ExtendedPrice"]

To put this in a table I use "<td>" + dr["ExtendedPrice"].ToString()
+ "</td>"

But I want to format the number as mentioned in the subject line.

It won't work...tells me that ToString accepts 1 argument.

What am I doing wrong?

Thanks-in-Advance,

Bob Sweeney

Hey Bob,

The DataRow index operator returns an object. So the ToString method
is the ToString method if the object returned.
If you need to format the returned object, use String.Format()
overloads.
For instance, to format the price in currency use
String.Format("{0:C}", dr["ExtendedPrice"]);

Refer to
http://msdn2.microsoft.com/en-us/library/system.string.format.aspx
for further details and advanced formatting.

Cheers,
Moty
 
Hello Folks,
I am bring backa data reader dr from the database/
So I have a number of fields such as dr["ExtendedPrice"]
To put this in a table I use "<td>" + dr["ExtendedPrice"].ToString()
+ "</td>"
But I want to format the number as mentioned in the subject line.
It won't work...tells me that ToString accepts 1 argument.
What am I doing wrong?

Bob Sweeney

Hey Bob,

The DataRow index operator returns an object. So the ToString method
is the ToString method if the object returned.
If you need to format the returned object, use String.Format()
overloads.
For instance, to format the price in currency use
String.Format("{0:C}", dr["ExtendedPrice"]);

Refer tohttp://msdn2.microsoft.com/en-us/library/system.string.format.aspx
for further details and advanced formatting.

Cheers,
Moty

Of course, you can always use unboxing as my colleagues just
suggested :)

Moty
 

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