how to perform division in timespan [duplicate] - c#

This question already has answers here:
What is the best way to divide two TimeSpan objects?
(4 answers)
Closed 5 years ago.
I have a value in TimeSpan, let's say: tsp1 = 2 hour 5 minutes.
I have another TimeSpan variable which contains a value like: tsp2 = 0 hours 2 minutes
Please tell me how I can divide tsp1 by tsp2 so that I can get the exact number of times tsp2 divides into tsp1 and what the remainder is.
I am using Visual Studio 2008.
Thanks.

The simplest approach is probably just to take their lengths in ticks, and divide those. For example:
long ticks1 = tsp1.Ticks;
long ticks2 = tsp2.Ticks;
long remainder;
long count = Math.DivRem(ticks1, ticks2, out remainder);
TimeSpan remainderSpan = TimeSpan.FromTicks(remainder);
Console.WriteLine("tsp1/tsp2 = {0}, remainder {1}", count, remainderSpan);

a div b:
double adivb = (double)a.Ticks/b.Ticks;
edited:
i found another post on th same topic
How can I achieve a modulus operation with System.TimeSpan values, without looping?

An int will hold enough seconds for ~64 years, so as long as you stay well below that:
int count = (int) (tsp1.t.TotalSeconds / tsp2.t.TotalSeconds);
double remainder = tsp1.t.TotalSeconds - (count * tsp2.t.TotalSeconds);
And maybe convert the remainder to int as well.

Related

How to Calculate with TimeSpan in c#

I need to divide two TimeSpan.
I have one TimeSpan "worktime" and the other TimeSpan "productive"
What i want is to get as result of worktime/productive is a percentage. I need to get the "added value" (<- I think this is how it's called in english :)),
In .NET Core you can simply divide the TimeSpans. TimeSpan has a division operator.
This works:
var t1=TimeSpan.FromTicks(1000);
var t2=TimeSpan.FromTicks(100);
Console.WriteLine(t2/t1);
---------------------
0.1 ```
A Simple example:
DateTime d1 = DateTime.Parse("10:00:00");
DateTime d2 = DateTime.Parse("13:00:00");
TimeSpan t1 = d2 - d1; // three hours
TimeSpan t2 = new TimeSpan(0,15,0); // 15 minutes
Console.WriteLine(t1/t2);
This outputs: 12
see: https://onlinegdb.com/ImxwiWk37
var addedValue = (decimal)workTime.Ticks/productive.Ticks * 100;
The decimal cast is needed or the division will automatically round and you'll get very inaccurate percentages in certain cases.
I need to get the "added value"
By this, do you mean you want the percentage expressed as a percentage of the overall work time that was productive?
So if the total work time was 10 hours and the productive work time was 9 hours, the percentage of productive work time would be 90%?
If so, the answer is:
double percentage = double percentage = 100.0 - 100.0 * (worktime.Ticks - productive.Ticks) / worktime.Ticks;

Compare timespan to integer [duplicate]

This question already has an answer here:
Get days as an int from a timespan?
(1 answer)
Closed 4 years ago.
Is it possible to compare a timespan to an integer in C#?
if say I want to know if a timespan is equal to 30 days, and if it is then do something?
There is the property TotalDays
There's also TotalHours, TotalMinutes, TotalSeconds and TotalMilliseconds. You should check out the TimeSpan-properties for more information
You can use the property Days of the TimeSpan object, which returns the days component of the time interval
if (ts.Days == 30)
{
// do something
}
TimeSpan t = new TimeSpan();
if(t.TotalDays==30)
{
//Do Something
}

floats being saved as 0 after calculation [duplicate]

This question already has answers here:
Why do these division equations result in zero?
(10 answers)
Closed 5 years ago.
Very confused here, I have this code:
float temp = (1/10)*100;
Label.Text = Convert.ToString(temp);
for some reason my temp variable is being saved as 0 which means my label text is changed to 0 when I'm expecting 10, I have the same issue when using doubles instead. What has gone wrong?
Since 1, 10 and 100 are all integer values, the division is also an int value, rounded down. In this case 1/10 = 0 so (1/10)*100 = 0
If you do not want this try using floats:
(1.0f/10)*100
In case you're working with integer variables you have to convert them first. This can be achieved by casting, like so:
int a=1;
...
float b = ((float) a)/10; // b will be 0.1
Another quick way of doing this in a line with multiple operations is multiplying by 1.0f:
int x = 100;
float c = (a*1.0f)/x; // c will be 0.01f

Difference between 2 days in years, months and days [duplicate]

This question already has answers here:
Date difference in years using C# [duplicate]
(18 answers)
Closed 8 years ago.
I need to output the difference between 2 days in years, months and days.
With timespan and subtract i only get the days but is it possible to output in years, months and days.
This is what i have so far:
public string Leeftijdsverschil(DateTime p1, DateTime p2)
{
if (p1 > p2)
{
DateTime verschil = p1.Subtract(TimeSpan.FromTicks(p2.Ticks));
return verschil.ToString();
}
else
{
DateTime verschil = p2.Subtract(TimeSpan.FromTicks(p1.Ticks));
return verschil.ToString();
}
}
The .NET types don't give you a decent answer to this without having to write code to try one answer and adjust appropriately, but my Noda Time project is designed to handle this sort of thing. For example:
LocalDate x = new LocalDate(2013, 5, 21);
LocalDate y = new LocalDate(2014, 12, 15);
Period p = Period.Between(x, y);
Console.WriteLine("{0} {1} {2}", p.Years, p.Months, p.Days);
Result:
1 6 24
(i.e. 1 year, 6 months and 24 days)
If you decide to use Noda Time for this, you could just do it within your method, using DateTime elsewhere and converting between the types... but it would generally be better to embrace Noda Time more widely. The aim is for it to allow your code to be clearer than just using DateTime.
To get the delta, just subtract directly:
var delta = p1 - p2;
This gives you a TimeSpan. From there, you have access to .Days. Months and years, however, are much more complicated. If you want the difference in those terms, you'll have to compare the properties of p1 and p2 directly (or: as Jon says: use Noda Time)

C# - Having trouble dividing by a 0.x number [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
My C# program requires two numerical outputs.
A main price will be divided by 20% and 10%.
For example:
Main price = 200
Discounted price: 20% of 200 = 40. Therefore total price is now 160.
I am having issues converting decimals, int's, etc. Here is what I have tried.
int discountNumber = 20 / 100;
decimal DiscountedPrice = Convert.ToInt32(TotalPrice)
/ Convert.ToInt32(discountNumber);
txtTotalPrice.Text = DiscountedPrice.ToString();
The problem in the
int discountNumber = 20 / 100;
it's always zero
change the int to a double. 20/100 will give you a decimal figure not an int

Categories

Resources