Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
lblTotalHolidayEntitled.Text = Math.Round(decimal.Parse(objLeave.HolidayTotalThisYear.ToString()), 2).ToString();

this is what i am trying ... why i need to parsing .. if object value iteself is a decimal .. without that its not working
Posted
Updated 5-Sep-13 2:05am
v3

Wow :)

You take a decimal value, convert it to a string, then parse it again to a decimal to round it, and convert it back to string.

You could try :

C#
lblTotalHolidayEntitled.Text = objLeave.HolidayTotalThisYear.ToString("# ##0.00");


or

C#
lblTotalHolidayEntitled.Text = Math.Round(objLeave.HolidayTotalThisYear, 2).ToString();


This is much simpler and should do what you need.

EDIT after some more informations :

If HolidayTotalThisYear is a nullable type, then you have to do :

C#
if (objLeave.HolidayTotalThisYear.HasValue) {
   lblTotalHolidayEntitled.Text = Math.Round(objLeave.HolidayTotalThisYear.Value, 2).ToString();
}
 
Share this answer
 
v4
Comments
Torakami 5-Sep-13 7:57am    
what is # means ??
phil.o 5-Sep-13 8:03am    
See http://msdn.microsoft.com/en-us/library/0c899ak8.aspx (Custom Numeric Format Strings)
Torakami 5-Sep-13 7:58am    
lblTotalHolidayEntitled.Text = Math.Round(objLeave.HolidayTotalThisYear, 2).ToString();


i tried this before but its not accepting ... thats y i tried with parse ...
phil.o 5-Sep-13 8:02am    
What error is returned when you try ?
Torakami 5-Sep-13 8:05am    
has some invalid arguments .. may be coz my property contains "decimal? holidayTotalThisYear" this way
So run it through decimal.TryParse[^] instead of decimal.Parse - it checks it, does the conversion if it can and reports true or false whether the conversion succeeded or not, without throwing any exceptions.
 
Share this answer
 
The Static TryParse Method is to determine whether the representation of string is a specified valid numeric type or not. Below is the link gives in detail.

http://msdn.microsoft.com/en-us/library/bb384043.aspx[^]
 
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