I sometimes get requests to add functions to Console Calculator. Â I have many functions built-in to CCalc, but of course many more could be imagined. Â Therefor, I made it possible to define your own functions. Â I previously posted a short list of a few useful functions one might define: Â Useful Console Calculator functions.
I received a request today for a more advanced
round() function. Â CCalc already includes a
round() function, and it always rounds to the nearest integer. Â The request I received was to support rounding to a specified decimal place:
round(2.535) = 3
round(2.535, 1) = 2.5
round(2.535, 2) = 2.54
In CCalc, function names may be overloaded, similar to C++, such that
round(x) is a different function from
round(x,n) since they take different number of input arguments. Although
round(x) is a built-in CCalc function,
round(x,n) is not – so let’s define it!
round(x,n) = round(x*10^n)/(10^n)
This new round function will round the number
x to the nearest
n decimal places. Very nice!