I know many languages have the ability to round to a certain number of decimal places, such as with the Python:
>>> print round (123.123, 1)
123.1
>>> print round (123.123, -1)
120.0
But how do we round to an arbitrary resolution that is not a decimal multiple. For example, if I wanted to round a number to the nearest half or third so that:
123.123 rounded to nearest half is 123.0.
456.456 rounded to nearest half is 456.5.
789.789 rounded to nearest half is 790.0.
123.123 rounded to nearest third is 123.0.
456.456 rounded to nearest third is 456.333333333.
789.789 rounded to nearest third is 789.666666667.
You can round to an arbitrary resolution by simply scaling the number, which is multiplying the number by one divided by the resolution (or, easier, just dividing by the resolution).
Then you round it to the nearest integer, before scaling it back.
In Python (which is also a very good pseudo-code language), that would be:
In that above code, it’s the
roundPartialfunction which provides the functionality and it should be very easy to translate that into any procedural language with aroundfunction.The rest of it, basically a test harness, outputs: