[Solved] Parsing a TimeSpan


#1

Hello everybody,
I have got an input textbox, to seize a number of seconds.
I seize 180,095 in it, with French format, that makes 180.095 in American format.
I use it with such code :


			float floatSec = float.Parse(txbSource.Text);
			int nbSec = (int)Math.Floor((double)floatSec);
			int nbMilli = (int)(floatSec - Math.Floor((floatSec - nbSec) * 1000));
			TimeSpan ts = new TimeSpan(0, 0, nbSec, nbMilli);

Well, for floatSec I wait 180.095, and that is what it is.
For nbSec I wait 180, and that is what it is.
Well now, I want to obtain the number of milliseconds, so I subtract nbSec from floatSec, and after multiplying by 1000 I wait 95, right ?
Well, I obtain 85.
For sure a problem of rounding, I remember having seen that somewhere.
What do you propose as best solution to treat this ?


#2

Well sorry, much more simple as I came to it :


				string value = txbInput.Text;
                                float floatSec = float.Parse(value);
				int nbSec = (int)Math.Floor((double)floatSec);
				int nbMilli = (int)(Math.Round((floatSec - nbSec) * 1000));
				TimeSpan ts = new TimeSpan(0, 0, 0, nbSec, nbMilli);
				lblResultat.Text = String.Format("'{0}' --> {1}", value, ts);

#3

Well, maybe this can still be improved :
‘86595,467’ --> 1.00:03:15.4690000