Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
okay so I am using SQL Datareader and the code is below..
ltUP.Text = dr["UnitPrice"].ToString();

Now I wanted the "UnitPrice" to be shown in this format:
{0:#,###.00};

because in my database its in float... I wanted for it to be in money when I display it in my aspx file..

What I have tried:

I've tried these:
ltUP.Text = dr["UnitPrice"].ToString("{0:#,###.00}");

ltUP.Text = dr["UnitPrice","{0:#,###.00}"].ToString();

But I know its wrong.. any help would be great..
Posted
Updated 2-Aug-17 4:05am
Comments
CHill60 2-Aug-17 7:37am    
How do you "know" that it's wrong?
BasicProgrammer__ 2-Aug-17 7:44am    
Because the error message I get for both is "Compilation Error".....
Kornfeld Eliyahu Peter 2-Aug-17 7:38am    
Why not convert it to money in the first place and after that formatting?
What control do you use to display data?
Michael_Davies 2-Aug-17 7:43am    
Don't use float to store "money", you'll not get the right answers...

Try:
tUP.Text = ((double)dr["UnitPrice"]).ToString("{0:#,###.00}");
 
Share this answer
 
Comments
BasicProgrammer__ 2-Aug-17 7:44am    
Sadly it returned with "Unit Price 14,00:0,000.00"
BasicProgrammer__ 2-Aug-17 7:49am    
I tried fixing your code.. change it to:
ltUP.Text = ((double)dr["UnitPrice"]).ToString("#,###.00");

and it worked! Thank you
OriginalGriff 2-Aug-17 7:57am    
Doh! Yeah, I got confused - that's the String.Format formatting string! :blush:
Kornfeld Eliyahu Peter 2-Aug-17 8:46am    
For next time (instead of try and fail):
https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
I suppose you should retrieve the actual value, using the GetDouble[^] method (yes, a SQL FLOAT correspinds to a C# double) method and then format it according to your needs.
 
Share this answer
 
try like this below
ltUP.Text = ((double)dr["UnitPrice"]).ToString("#,###.00");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900