public static double RoundNumberUpBy(double number, double roundBy) => Math.Ceiling(number * 1.0 / roundBy) * roundBy; // Only add up
public static double RoundNumberBy(double number, double roundBy) => Math.Round(number * 1.0 / roundBy) * roundBy;
void Main()
{
Console.WriteLine(RoundNumberBy(0, 15));
Console.WriteLine(RoundNumberBy(1, 15));
Console.WriteLine(RoundNumberBy(20, 15));
Console.WriteLine(RoundNumberBy(25, 15));
Console.WriteLine(RoundNumberBy(35, 15));
Console.WriteLine(RoundNumberBy(40, 15));
Console.WriteLine(RoundNumberBy(55, 15));
Console.WriteLine(RoundNumberBy(65, 15));
}
/*
Results in:
0
0
15
30
30
45
60
60
*/
Typescript
export const roundNumberBy = (number: number, roundBy: number) => Math.round(number * 1.0 / roundBy) * roundBy;
export const roundNumberUpBy = (number: number, roundBy: number) => Math.ceil(number * 1.0 / roundBy) * roundBy;
const minutesToTime = (m: number) => `${String(Math.floor(m / 60)).padStart(2, "0")}:${String(m % 60).padStart(2, "0")}`;
const setNextHourTime = () => {
var now = new Date();
var startTimeInMinutes = (now.getHours() * 60) + now.getMinutes();
startTimeInMinutes = roundNumberBy(startTimeInMinutes, 15);
var endTimeInMinutes = startTimeInMinutes + 60;
setEventStart(minutesToTime(startTimeInMinutes));
setEventEnd(minutesToTime(endTimeInMinutes));
};477200cookie-checkC# / Typescript Round Number By