By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

May 10 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
May 10 '07 #2

P: n/a
"Bob" <Go****@Yahoo.Comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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>"

--
http://www.markrae.net

May 10 '07 #3

P: n/a
Kick myself!! Prices as double! Sheesh! I meant decimal....

the shame...

Marc
May 10 '07 #4

P: n/a
On May 10, 1:17 pm, Bob <Go1...@Yahoo.Comwrote:
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/lib...ng.format.aspx
for further details and advanced formatting.

Cheers,
Moty
May 10 '07 #5

P: n/a
On May 10, 1:37 pm, Moty Michaely <Moty...@gmail.comwrote:
On May 10, 1:17 pm, Bob <Go1...@Yahoo.Comwrote:
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 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

May 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.