getMultipleOf

Given a number, calculate the largest multiple of n needed to reach that number.

It rounds up, and if supplied alwaysOneUp: true it will always overshoot. This is good for when calculating format pattern widths.

@safe pure nothrow @nogc
getMultipleOf
(
Number
)
(
const Number num
,
const int n
,
const bool alwaysOneUp = false
)

Parameters

num Number

Number to reach.

n int

Base value to find a multiplier for.

alwaysOneUp bool

Whether or not to always overshoot.

Return Value

Type: auto

The multiple of n that reaches and possibly overshoots num.

Examples

immutable width = 15.getMultipleOf(4);
assert(width == 16);
immutable width2 = 16.getMultipleOf(4, alwaysOneUp: true);
assert(width2 == 20);
import std.conv : text;

immutable n1 = 15.getMultipleOf(4);
assert((n1 == 16), n1.text);

immutable n2 = 16.getMultipleOf(4, alwaysOneUp: true);
assert((n2 == 20), n2.text);

immutable n3 = 16.getMultipleOf(4);
assert((n3 == 16), n3.text);
immutable n4 = 0.getMultipleOf(5);
assert((n4 == 0), n4.text);

immutable n5 = 1.getMultipleOf(1);
assert((n5 == 1), n5.text);

immutable n6 = 1.getMultipleOf(1, alwaysOneUp: true);
assert((n6 == 2), n6.text);

immutable n7 = 5.getMultipleOf(5, alwaysOneUp: true);
assert((n7 == 6), n7.text);

immutable n8 = 5L.getMultipleOf(5L, alwaysOneUp: true);
assert((n8 == 6L), n8.text);

immutable n9 = 5UL.getMultipleOf(5UL, alwaysOneUp: false);
assert((n9 == 5UL), n9.text);

immutable n10 = (5.0).getMultipleOf(5UL, alwaysOneUp: true);
assert((n10 == (6.0)), n10.text);

Meta