473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 70247
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
5
by: geskerrett | last post by:
We are working on a project to decipher a record structure of an old accounting system that originates from the late80's mid-90's. We have come across a number format that appears to be a "float"...
13
by: - Steve - | last post by:
I've been given a school assignment and while everything else is easy there's one topic I'm completley lost on. I've been given an ASCII file that looks like this. During start-up, the program...
8
by: Tony | last post by:
Hello I am learning C# and encountered the following problem when I tried to figure out how to print the string {0} in a Console window The following piece of codes complied OK. But when I...
7
by: Stimp | last post by:
I want to output a number, say 20, as 20.00 i.e. I want to always have 2 decimal places I don't want any currency information, therefore String.Format("{0:c}") doesn't work for me. Can't...
6
by: Bob | last post by:
Hello folks. I use this formating but if the value in dr is a null it generates an error "Object cannot be cast from DBNull to other types." Anybody know how I can deal with nulls? ...
5
by: buddyr | last post by:
Hello I am working on time sheet form. Whether I create fields in query = \60 & format( mod 60, "\00") or Round((Table2.OUT-Table2.)*24,4) AS HOURS When I try to add my totals in the fields to...
3
by: Imaginativeone | last post by:
XML <nodeAA>AA</nodeAA> <nodeBB>BB</nodeBB> <nodeCC> <From>12/05</From> <To>11/06</To> <Months>12</Months> <Amount>10.00</Amount> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.