tags:

views:

95

answers:

1

I need a mathematical function/formula/expression to take a number as parameter and find the lowest number ending with certain digits that is larger than the parameter.

For instance, if the ending digits should be 88 these are some examples of what I want:

f(0) = 0
f(87) = 88
f(88) = 88
f(89) = 188  //NB: NOT 88*2=176
f(187) = 188
f(188) = 188
f(189) = 288

and so on. You get the idea.

So far I've used a function (in Delphi, not implemented by me) that does this:

function RoundToSpecificEndingDigits(aLength,aModMeasure : double) : double;
begin
  Result := aModMeasure;
  while Result < aLength do Result := Result + 100;
end;

Now I need a more "mathematical" approach with divs and mods and rounds etc. The reason is that I want to do it in SQL without creating functions.

+1  A: 

how about something like this:

  1. count the number of digits you want to round to "88" => n = 2
  2. subtract the suffix from your number.
  3. round up to the nearest 10^n (divide by 10^n, round to integer, multiply by 10^n)
  4. add the suffix.

In SQL:

SELECT
    CEIL(
        (num - suffix) / POW(10, LENGTH(suffix))
    ) * POW(10, LENGTH(suffix)) + suffix
nickf