Member 12349103 Ответов: 2

Как мне округлить секунды


Я видел много вариантов roundup, но приведенный ниже код должен работать, я что-то упустил? Я хочу, чтобы все было просто. Спасибо



<pre> if (calculated_start_time.Second >= 30)
                    Delivery_time = calculated_start_time.AddMinutes(1).ToString("HH:mm:ss");
                else

                    Delivery_time = calculated_start_time.ToString("HH:mm:ss");


Что я уже пробовал:

Вариации параметров сводка
Интернет

Afzaal Ahmad Zeeshan

И в чем проблема?

Вы хотите только упростить код?

2 Ответов

Рейтинг:
6

Peter Leow

Если я правильно вас понял, вы хотите округлить секунду до следующей минуты, когда она равна 30 или более, для этого вам просто нужно добавить оставшуюся секунду к следующей минуте, вычитая эту секунду из 60 секунд. Например:

using System;
					
public class Program
{
	public static void Main()
	{
		DateTime startDateTime = DateTime.Now;
				
		Console.WriteLine("Before rounding: " +  startDateTime.ToString("HH:mm:ss"));
		
		DateTime delivery_time = startDateTime;  
		int second = startDateTime.Second;
		if( second >= 30)
		{
			delivery_time = startDateTime.AddSeconds(60-second);
			
		}
		
		Console.WriteLine("After rounding : " + delivery_time.ToString("HH:mm:ss"));
		
	}
}
и пример результатов:
Before rounding: 06:28:38
After rounding : 06:29:00

Before rounding: 06:29:04
After rounding : 06:29:04


Рейтинг:
12

Wendelius

Поскольку вы также отображаете секунды (...ToString ("HH:mm: ss")), вам, вероятно, также нужно установить секунды на ноль, иначе вы просто сдвинете минуту.

Рассмотреть следующее

System.DateTime time = new DateTime(2017, 1, 11, 23, 35, 28);

for (int counter = 0; counter < 70; counter++) {
   // increment time
   time = time.AddSeconds(1);
   // print the rounded time
   Console.WriteLine( string.Format("Original time {0}, rounded time {1}",
      time.ToLongTimeString(),
      time.AddMinutes(time.Second < 30 ? 0 : 1)
          .AddSeconds(time.Second * -1).ToLongTimeString()));
}

Результат будет выглядеть следующим образом
Original time 23:35:29, rounded time 23:35:00
Original time 23:35:30, rounded time 23:36:00
Original time 23:35:31, rounded time 23:36:00
Original time 23:35:32, rounded time 23:36:00
Original time 23:35:33, rounded time 23:36:00
Original time 23:35:34, rounded time 23:36:00
Original time 23:35:35, rounded time 23:36:00
Original time 23:35:36, rounded time 23:36:00
Original time 23:35:37, rounded time 23:36:00
Original time 23:35:38, rounded time 23:36:00
Original time 23:35:39, rounded time 23:36:00
Original time 23:35:40, rounded time 23:36:00
Original time 23:35:41, rounded time 23:36:00
Original time 23:35:42, rounded time 23:36:00
Original time 23:35:43, rounded time 23:36:00
Original time 23:35:44, rounded time 23:36:00
Original time 23:35:45, rounded time 23:36:00
Original time 23:35:46, rounded time 23:36:00
Original time 23:35:47, rounded time 23:36:00
Original time 23:35:48, rounded time 23:36:00
Original time 23:35:49, rounded time 23:36:00
Original time 23:35:50, rounded time 23:36:00
Original time 23:35:51, rounded time 23:36:00
Original time 23:35:52, rounded time 23:36:00
Original time 23:35:53, rounded time 23:36:00
Original time 23:35:54, rounded time 23:36:00
Original time 23:35:55, rounded time 23:36:00
Original time 23:35:56, rounded time 23:36:00
Original time 23:35:57, rounded time 23:36:00
Original time 23:35:58, rounded time 23:36:00
Original time 23:35:59, rounded time 23:36:00
Original time 23:36:00, rounded time 23:36:00
Original time 23:36:01, rounded time 23:36:00
Original time 23:36:02, rounded time 23:36:00
Original time 23:36:03, rounded time 23:36:00
Original time 23:36:04, rounded time 23:36:00
Original time 23:36:05, rounded time 23:36:00
Original time 23:36:06, rounded time 23:36:00
Original time 23:36:07, rounded time 23:36:00
Original time 23:36:08, rounded time 23:36:00
Original time 23:36:09, rounded time 23:36:00
Original time 23:36:10, rounded time 23:36:00
Original time 23:36:11, rounded time 23:36:00
Original time 23:36:12, rounded time 23:36:00
Original time 23:36:13, rounded time 23:36:00
Original time 23:36:14, rounded time 23:36:00
Original time 23:36:15, rounded time 23:36:00
Original time 23:36:16, rounded time 23:36:00
Original time 23:36:17, rounded time 23:36:00
Original time 23:36:18, rounded time 23:36:00
Original time 23:36:19, rounded time 23:36:00
Original time 23:36:20, rounded time 23:36:00
Original time 23:36:21, rounded time 23:36:00
Original time 23:36:22, rounded time 23:36:00
Original time 23:36:23, rounded time 23:36:00
Original time 23:36:24, rounded time 23:36:00
Original time 23:36:25, rounded time 23:36:00
Original time 23:36:26, rounded time 23:36:00
Original time 23:36:27, rounded time 23:36:00
Original time 23:36:28, rounded time 23:36:00
Original time 23:36:29, rounded time 23:36:00
Original time 23:36:30, rounded time 23:37:00
Original time 23:36:31, rounded time 23:37:00
Original time 23:36:32, rounded time 23:37:00
Original time 23:36:33, rounded time 23:37:00
Original time 23:36:34, rounded time 23:37:00
Original time 23:36:35, rounded time 23:37:00
Original time 23:36:36, rounded time 23:37:00
Original time 23:36:37, rounded time 23:37:00
Original time 23:36:38, rounded time 23:37:00

Конечно, на вывод влияют Мои настройки локали, поэтому, если вам нужен фиксированный формат, то вполне возможно использовать пользовательскую модель формата.