Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a proc with return a float as
100.00

when i assign this float to textbox ,it is showing only 100 without decimal number.

txtTotalPurCost.Text = float.Parse(objdto[0].Tot_Pur_Cost.ToString().TrimStart().TrimEnd());
Posted
Comments
Sinisa Hajnal 5-Sep-14 6:40am    
Next time search for the solution online BEFORE posting the question. This could've been very easily found elsewhere.

Try:
C#
txtTotalPurCost.Text = float.Parse(objdto[0].Tot_Pur_Cost.ToString().Trim()).ToString("0.##");
The format string says to always show two decimal places.
 
Share this answer
 
Comments
Sinisa Hajnal 5-Sep-14 6:42am    
Change # into 0 or it will NOT give two decimals if there are none. For example: 10.1 with #.## will give 10.1 while format 0.00 will give 10.10
you can use the Custom numeric formts like
but this will give when you try 0.00 empty string.

Ex :
C#
Float I= 100.00
string str=I.ToString ("#.##");

Your one:
C#
txtTotalPurCost.Text = float.Parse(objdto[0].Tot_Pur_Cost.ToString ("#.##");
.TrimStart().TrimEnd());


for more info

http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Sinisa Hajnal 5-Sep-14 6:46am    
Change # into 0 or it will NOT give two decimals if there are none. For example: 10.1 with #.## will give 10.1 while format 0.00 will give 10.10
C#
decimal d = 1.000M;
        string s = d.ToString("#.##");
        txt1.Text = s;
 
Share this answer
 
Add "0.00" into to string like this: .ToString("0.00"). Also, you don't need TrimStart and TrimEnd. There is a function Trim that does that. But you don't need trim at all, your number will be parsed then turned into string without extra characters.

If this solves your problem, please accept the solution so that others may find it. Thank you.
 
Share this answer
 
v3
Comments
Sinisa Hajnal 10-Sep-14 3:57am    
Why 1-vote?

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