Rounding & Truncating

Searching the forum, I found a way to round a value to 2 decimal places (useful in CL documentation?)

rounded_value = simpleFunction("\operatorname{round}(k,2)",“k”).evaluateAt(value)

Using this I can also round to an integer by setting the number of decimal places to zero but is there a better way?

Also, is there an operator for ‘truncate’? I tried that and ‘trunc’ in the above but no success.

This would round to the nearest integer, but you could also use a graph if you have one available in the activity.

rounded_value = simpleFunction("\operatorname{round}(k)",“k”).evaluateAt(value)

As for truncate, I don’t believe there is such a function. There is a ceiling and floor function, so if you know the signed value, you can use one of those.

Perfect! Thank you for your help.

I realize that cwinske already addressed your question, however, I uncovered some new code on a more recent post that may also be of interest to you (see https://cl.desmos.com/t/answer-that-is-a-fraction/1077 for more information). I was struggling to round values within a table and found the following code quite helpful:

rounded_value=numericValue("\round(${value},2)")

The code was helpful for me because it didn’t require simpleFunction.

1 Like

Thanks Todd, that’s very helpful and interesting! Blockquote took out one of the backslashes before the “round” (there should be two) but when added that code is fine and is certainly much neater. I also tried using “floor” and “ceiling” in place of “round” but that gave me “NaN”?

Rounding to the nearest integer with the following code was also fine.

rounded_value=numericValue("\\round(${value})")

A full explanation of what is possible with this format would be useful? …especially if added to the documentation.

Both simpleFunction and numericValue can do similar things. I’ve started to use simpleFunction more frequently because I ran into some issues with numericValue doing some incorrect calculations with numbers in scientific notation. Apparently it does this once in a while (I think Jay mentioned it somewhere on here). However, numericValue should run fine with your functions.

To get floor or ceiling, try this:

numericValue("\\operatorname{floor}(${value})")
numericValue("\\operatorname{ceil}(${value})")

Sorry, I had experimented more than I described and found ‘floor’ worked when looking for an integer but objected to a “,2” being added if I wanted a number to two decimal places. I do know how to achieve the same effect so it is not an important restriction but I do wish that CL was easier to predict.

These are not specifically CL functions, they’re Desmos calculator functions. You can search for things on the support site. Here’s a link for the floor and ceiling functions. Although I’ll be honest, it can be difficult sometimes to find exactly what you’re looking for.

1 Like

Thanks, looks like there are some more functions there to explore as well!

You can do explicit truncation with the following function:

T\left(x\right)=\operatorname{round}\left(x-\frac{\left|x\right|}{x}0.5\right)

No need to know the sign of the input in advance: Explicit Truncation


Need to be able to pick the number of digits of truncation and aren’t too worried about piecewise case handling?

T\left(x\right)=\left\{x\ge0:\frac{\operatorname{floor}\left(10^{n}x\right)}{10^{n}},x<0:\frac{\operatorname{ceil}\left(10^{n}x\right)}{10^{n}}\right\}

Here you go: Truncate