store zeros after decimal point in variable in c# -


my application in c# on payroll management system...and have work hours of each employee in variable ...for m using double ...suppose employee works 8 hours , 20 min ..then entry 8.20 in text box...so separating part before decimal in 1 variable using`

var values = totaldays.tostring(cultureinfo.invariantculture).split('.'); int firstno = int.parse(values[0]); int secondno = int.parse(values[1]);` 

so m getting first variable accurately if part after decimal contains zeros not storing in "secondno" variable . zeros eliminated automatically , result 8.20 , 8.2 same i.e., 8.2 .

but since time different 1 8 hours 20min , other 8 hours 2min ..i want solution to ..please me since whole application dependent on this.

if user entering hours , minutes, why not use timespan?

var input = "8.20"; var time = timespan.parseexact(input, @"h\.mm", null); var hours = time.hours;      // 8 var minutes = time.minutes;  // 20 

further reading


if must store decimal first, can still manage using this:

var input = 8.20m; var parts = input.tostring("0.00", cultureinfo.invariantculture).split('.'); var hours = int.parse(parts[0]);     // 8 var minutes =  int.parse(parts[1]);  // 20 

but ellesedil points out, you'll have decide how handle values 8.70.


Comments