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.

1 Like

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

I’d like to add a question to this topic as the notation is a bit unclear to me. The activity I am working on is this: Segment Addition • Activity Builder by Desmos

On screen 11, I want to take the angle measures from the graph component and put them in the table… as integers. The code I tried based on this thread was:

 cellContent(1,2):"numericValue("\round(${graph11.number(`f`)})"

Could someone sort of explain what the components of the rounding formula are supposed to do and what I might do to correct this problem?

Second question: This thread was 7 months old but relevant to what I am working on. Is it better to wake an old post or start a new one?

Basic format is “\round(number, decimal places)”. If you don’t have the second parameter, it rounds to the nearest integer. Your issue is that you need ${ } around the whole numericValue code, otherwise it’s just treated as text.

cellContent(1,2):"${numericValue("\round(${graph11.number(`f`)})}"

Personally, I would say start a new thread 9 times out of 10.

Thank you very much. Based on your response, I tried to break it down further to see what was happening. To better visualize for myself what is happening at each step, this is what I did :

s=numericValue("${graph11.number(z_4)}")

s_round="\round(${s})"

cellContent(1,2):"${s_round}"

Maybe this will help others going forward since multiple nested brackets could get confusing. Thank you again for your help. :slight_smile:

1 Like

You actually want to avoid this step:
s=numericValue("${graph11.number(z_4)}")

using s=graph11.number("z_4") is fine, but what you’re actually doing with this numericValue function is taking a number, converting it into a string, then taking the numeric value of that string. For example, we print .000000033 as 3.3e-8. Then, numericValue will interpret that as “3.3 times e, minus 8” or 0.970330033915.

Try entering some some small numbers here and you’ll see that sometimes the numeric value and the numericValue of the numeric value are different.

Based on your example, I see what you mean. Thanks. The activity linked here seems to be empty though.

strange! I re-published, try again?

Yep that did it. I appreciate you taking the time to build this example.