For example suppose I have a method
double roundDown(double unrounded, double precision) {
}
roundDown(137.42973, 0.01) would return 137.42
roundDown(137.42973, 0.05) would return 137.4
roundDown(137.42973, 0.25) would return 137.25
Are there libraries to do something like this otherwise is the algorithm reasonably simple?
Divide the original number by the precision, round down to the nearest integer, and then multiply by the precision again:
Note that inherent floating point inaccuracies mean that your number may not be exactly the answer you’re looking for – e.g. you might get 137.399999 instead of 137.4