Click here to Skip to main content
15,920,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an text box called planned hours which accepts hours. It will allow only numeric and decimal values.And only single decimal is allowed.
So example the valid values are:

1.5-- ok
1-- ok
12.5-- ok
12 -- ok
12.0-- ok
1.0-- ok

So the max length of textbox is 4.

I have two screens:
1.Create plan
2.View plan

while creating the plan when i pass planned hour text box value as 12.5, in plan view screen, the planned hour value is displayed correctly(i.e 12.5) and zero is not appended after 5

but when i pass planned hour text box value as 1.5 or 1, in plan view screen, the planned hour value is displayed 1.50 or 1.00

Am using the below condition in plan view screen:

txtPlannedHours.Text = PlannedTime.ToString().Remove(4);


since i have passed a value as 4 in remove string method, its appending a value as zero when the planned hour length is only 3 .

so how can i resolve this issue? so that it should display correct value as what ever was entered while creating in C#.


So when 12.5 was entered while creating, even in view it should be displayed as 12.
and when 1 or 1.4 was entered while creating even in that case it should just display as 1 or 1.4 instead of 1.00 or 1.40
Posted

Assuming that PlannedTime is a numeric of some sort the best way to do this is to provide a format string as part of the ToString method call, rather than try to trim the output:
C#
txtPlannedHours.Text = PlannedTime.ToString("0");
Should do it.
 
Share this answer
 
try the below code

txtPlannedHours.Text = PlannedTime.ToString().Remove(4).TrimEnd('0');
 
Share this answer
 
Comments
Member 10593922 6-Mar-14 6:12am    
Thank u for the solution.. but in my text box when user has entered a value as just 12 or 1, i have added on more event which appends ".0" as per our requirement to planned hour textbox. so in that case it is just displaying as 12. instead of 12.0 in plan view screen.. how can i solve that issue?

if 12.0 is entered, it should display as 12.0 only. and when 1.4 is entered it should display 1.4 only instead of 1.40, in this case only i want to remove 0 when the max length of text box was entered as 3

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