# Round Numbers. Make the Time numbers nice

Hello Guys
I hope you are well

I have to round these times to nice numbers:

If its 13 becomes 15, to nearest quarter.
I can read the text and convert it to integer and split it to minutes, and hour.

Do i have to use 100 if statements?

Thank You

one of many approaches

one question left: do you also looking for down rounding, like 15:17 → 15:15?

1 Like

Thank You Peter.
I am looking for:

08:13 → 08.15
14:17 → 14:15
19:29 → 19:30
13:24 → 13:25

Hi Hurmet,

`MinutesRoundedToQuarter (Type Double) = Math.Ceiling(SampleMinutes / 15) * 15`

OH! You want to round down too…

`MinutesRoundedToQuarter (Type Double) = Math.Round(SampleMinutes / 15) * 15`

Lukas

Perfect.
Now I have to check if its 60 then add one more to the time.

if i have 19:34

How can i substring “9”

Thank You Lukaszie

have a look on my samples as this was exactly handling tem 60 min case und jumps up to next hour

1 Like

Oh what, wait. That’s not good.

You should use DateTime.AddMinutes instead then! No more time right now, I’ll try later

1 Like

I am looking also for down …
When its 18:11 should be 18:10

Thank You

combine the approach from Lukasz and mine. Just give me a little time

1 Like

find starter help:
RoundTimesToClosest15MinSegment.xaml (7.7 KB)

2 Likes

Thank You Peter
I will test it now

Regards
Hurmet

I am looking for 5 minutes segment.

11 becomes 10
14 becomes 15
27 becomes 25
32 becomes 30

Thank You

so you are looking for a round to the closest 5 segement and not 15?

3 Likes

Yes Sir

This works. I changed 15 to 5 and it works.

Thank You MAN!

Only thing is how to convert “08:30” string to date?

`RoundedTime = SampleTime.AddMinutes((Math.Round(SampleTime.Minute / MinuteInterval ) * MinuteInterval) - SampleTime.Minute)`

You can convert strings to dates a number of ways. Lots of examples in the forum. Here is one:

`SampleDateTime = DateTime.ParseExact(SampleString,"HH:mm",CultureInfo.InvariantCulture)`

Cheers,
Lukas

3 Likes

You are correct, this Works

THANK YOU SIR

All the best

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.