How to add floating numbers in the form of hours, and the output hours - minutes and seconds

I have a simple problem, I have time in double format. i.e. 1.20 hours 2.30 hours 3.40 hours.

I want to add them as time clocks, not as floating numbers, that is, to base 60. for example

1.20 + 2.30 + 3.40 = 7.30 hours (correct)

1.20 + 2.30 + 3.40 = 6.90 (incorrect)

I want this in both SQL and C #, I am currently using custom functions on both sides. I want to know the easiest way to do this.

If I store it in the wrong format, please tell me how to do it correctly, basically I get the difference between the two dates as a floating point number.

We welcome all possible solutions.

+3
source share
6

, "" (: ) . .

  • : " " - , (x 100) -
  • .
  • " "
  • : + /100
0
double wholeHours = Math.Floor(timeVal);
TimeSpan realTime = new TimeSpan(wholeHours, (timeSpan - wholeHours) * 100 / 60;

"sql", TIME, ( #), SQLServer, , , TIME 2008 , , float

FLOOR(timeVal) + (timeVal - FLOOR(timeVal)) * 100 / 60
0

SQL. ConvertedData . float:

select  sum(hours) + sum(minutes) / 60 as TotalHours
,       sum(minutes) % 60 as TotalMinutes
,       sum(hours) + sum(minutes) / 60 + sum(minutes) % 60.0 / 100 as YourDt
from    (
        select  cast(YourDt % 1 * 100 as int) as minutes
        ,       cast(YourDt as int) as hours
        from    (
                select  1.2 as YourDt
                union all
                select  2.3
                union all
                select  3.4
                ) SourceData
        ) ConvertedData

:

TotalHours  TotalMinutes  YourDt
7           30            7.300000
0

, - . , . .

0

, .NET SQL, #

TimeSpan , , :

public static TimeSpan AddMinutes(this TimeSpan span, double mins)
{
  return span.Add(TimeSpan.FromMinutes(mins));
}

public static string ToHourAndMins(this TimeSpan span)
{
  return string.Format("{0}:{1:00}", Math.Truncate(span.TotalHours), span.Minutes);
}

:

var ts = TimeSpan.FromMinutes(20);
ts = ts.AddMinutes(45);
Console.WriteLine(ts.ToHourAndMins());
0

. :

    double original = 1.55d+2.9d+3.48d;
    double carry = (int)(original * 100) % 100 / 60;
    double result = original + carry - carry / 100 * 60;
0

Source: https://habr.com/ru/post/1767663/


All Articles