469,322 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

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
5 68999
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
"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
Kick myself!! Prices as double! Sheesh! I meant decimal....

the shame...

Marc
May 10 '07 #4
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
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.

Similar topics

43 posts views Thread by steve | last post: by
13 posts views Thread by - Steve - | last post: by
8 posts views Thread by Tony | last post: by
7 posts views Thread by Stimp | last post: by
3 posts views Thread by Imaginativeone | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.