Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Format function

Author  Topic 

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-03-08 : 11:53:59
Hi all,

How can i get $ 12.00 in a labels text?
label1.text = dt.Rows(0)("price").ToString---> returns 12.00000
i want to correct it to 2 deciamls and also add a dollar sign.

Can anyone help me in this?
Thanks

dfiala
Posting Yak Master

116 Posts

Posted - 2007-03-08 : 12:06:25
Label1.text = dt.Rows(0)("price").ToString("c")

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-08 : 12:32:47
If dt is a datatable, then rows(0) will return an object. So, that ToString() method overload will not work since Object doesn't have that overload. You'd need to cast it to the proper datatype first (i.e., decimal), and then use ToString() in that manner.

I find that it is easiest to simply use the static String.Format() method:

label1.text = String.Format("{0:c}", dt.Rows(0))


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

dfiala
Posting Yak Master

116 Posts

Posted - 2007-03-08 : 12:37:37
label1.text = String.Format("{0:c}", dt.Rows(0)("price"))

Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-03-08 : 15:31:14
oops, yes, missed the column reference, thank you.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

reflex2dotnet
Yak Posting Veteran

99 Posts

Posted - 2007-03-08 : 16:00:07
Thanks to all
We can also get it as
FormatCurrency(dt.rows(0)("price"))

Thanks
Go to Top of Page
   

- Advertisement -