pruuylan
pruuylan

Reputation: 86

How to convert timespan to decimal?

I have a value of exactly 1.08:43:23 in my textbox, which is equal to 1d, 08:43:23. I wanted to convert the value to decimal for me to multiply it by another decimal value, however when I used Convert.ToDecimal it returns the error

Input string is not a valid format

Is Convert.ToDecimal not appropriate for this kind of conversion ? Is there a way to create a decimal value from such input?

Upvotes: 7

Views: 17318

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

Is Convert.ToDecimal is not appropriate for this kind of conversion ?

No. You need to parse it to TimeSpan first (with a culture that has : as a TimeSeparator of course.). Then you can get which duration type do you want as a double from it.

var ts = TimeSpan.Parse("1.08:43:23", CultureInfo.InvariantCulture);

enter image description here

Then you can use TotalXXX properties which type duration do you want as a double (seconds, milliseconds etc.).

Upvotes: 17

Kishore Sahasranaman
Kishore Sahasranaman

Reputation: 4230

This will give u total number of ticks ..

decimal dec = Convert.ToDecimal(TimeSpan.Parse("11:30").Ticks);

Upvotes: 3

Related Questions