How to convert timespan to decimal?
Asked Answered
T

2

7

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?

Tisbee answered 23/10, 2015 at 7:15 Comment(4)
You can use TotalSeconds property of timespanWaki
Which decimal do you think corresponds to 1d 08:43:32? Are you converting the string to decimal, or a timespan object to decimal?Richy
What do you hope to gain by multiplying a length of time with another?Carbrey
Am working with some kind of payroll system, I wanted to get the earning based on timespanTisbee
L
17

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.).

Lowpitched answered 23/10, 2015 at 7:19 Comment(6)
This works too, thank you so much sir. Though has the same idea as above, I'll commend yah still, mind if I ask you a question ?Tisbee
@SuperPruuylan Sure, that's what comment section are for.Intestinal
what compiler are you using sir ?Tisbee
@SuperPruuylan Visual Studio Ultimate 2012. But I think you try to ask: "What theme are you using?" It is default Dark theme in VS which you can change it on Tools -> Change Color Theme -> DarkIntestinal
Also, the information in the picture can be seen during debugging. You need to set a breakpoint on the line you're interested in, and then hover over a variable to see values of all its fields and properties.Octonary
Am using Visual Studio Enterprise 2012, am using dark theme too but my fonts appears blue. lol I just loved your compiler, how it look likesTisbee
W
3

This will give u total number of ticks ..

decimal dec = Convert.ToDecimal(TimeSpan.Parse("11:30").Ticks);
Worser answered 23/10, 2015 at 7:17 Comment(1)
This works, thank you so much sir, can't tag "answered" yet. I'll tag it later, thank you so much, sirTisbee

© 2022 - 2024 — McMap. All rights reserved.