# Rounding & Truncating

**URL:** https://cl.desmos.com/t/rounding-truncating/1035
**Category:** Questions
**Created:** [May 18, 2020, 6:12am UTC](https://cl.desmos.com/t/rounding-truncating/1035 "2020-05-18T06:12:24Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Davy\_B](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/davy_b/32/3264_2.png) [@Davy\_B](https://cl.desmos.com/u/Davy_B)
#### Post date: [May 18, 2020, 6:12am UTC](https://cl.desmos.com/t/rounding-truncating/1035/1 "2020-05-18T06:12:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![cwinske](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/cwinske/32/3106_2.png) [@cwinske](https://cl.desmos.com/u/cwinske)
#### Post date: [May 18, 2020, 12:47pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/2 "2020-05-18T12:47:05Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Davy\_B](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/davy_b/32/3264_2.png) [@Davy\_B](https://cl.desmos.com/u/Davy_B)
#### Post date: [May 18, 2020, 3:36pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/3 "2020-05-18T15:36:28Z")

</div>

Perfect! Thank you for your help.

---

<div class="post-metadata">

### Author: ![Todd\_Edwards](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/todd_edwards/32/825_2.png) [@Todd\_Edwards](https://cl.desmos.com/u/Todd_Edwards)
#### Post date: [May 25, 2020, 2:51am UTC](https://cl.desmos.com/t/rounding-truncating/1035/4 "2020-05-25T02:51:34Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![Davy\_B](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/davy_b/32/3264_2.png) [@Davy\_B](https://cl.desmos.com/u/Davy_B)
#### Post date: [May 25, 2020, 8:00am UTC](https://cl.desmos.com/t/rounding-truncating/1035/5 "2020-05-25T08:00:31Z")

</div>

> [@Todd\_Edwards](#):
>
> rounded\_value=numericValue(“\round(${value},2)”)

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.

---

<div class="post-metadata">

### Author: ![cwinske](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/cwinske/32/3106_2.png) [@cwinske](https://cl.desmos.com/u/cwinske)
#### Post date: [May 25, 2020, 1:13pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/6 "2020-05-25T13:13:48Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![Davy\_B](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/davy_b/32/3264_2.png) [@Davy\_B](https://cl.desmos.com/u/Davy_B)
#### Post date: [May 25, 2020, 1:36pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/7 "2020-05-25T13:36:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Daniel\_Grubbs](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/daniel_grubbs/32/719_2.png) [@Daniel\_Grubbs](https://cl.desmos.com/u/Daniel_Grubbs)
#### Post date: [May 26, 2020, 7:34pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/8 "2020-05-26T19:34:42Z")

</div>

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](https://support.desmos.com/hc/en-us/articles/202547599-Floor-Greatest-Integer). Although I’ll be honest, it can be difficult sometimes to find exactly what you’re looking for.

---

<div class="post-metadata">

### Author: ![Davy\_B](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/davy_b/32/3264_2.png) [@Davy\_B](https://cl.desmos.com/u/Davy_B)
#### Post date: [May 27, 2020, 6:06am UTC](https://cl.desmos.com/t/rounding-truncating/1035/9 "2020-05-27T06:06:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Mike\_Gleeson](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/mike_gleeson/32/650_2.png) [@Mike\_Gleeson](https://cl.desmos.com/u/Mike_Gleeson)
#### Post date: [May 29, 2020, 2:21pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/10 "2020-05-29T14:21:27Z")

</div>

You can do explicit truncation with the following function:

```auto
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](https://www.desmos.com/calculator/hxgwjivixm)

* * *

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

```auto
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](https://www.desmos.com/calculator/cjzjho5bdd)

---

<div class="post-metadata">

### Author: ![Charlie\_Cannon](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/charlie_cannon/32/1708_2.png) [@Charlie\_Cannon](https://cl.desmos.com/u/Charlie_Cannon)
#### Post date: [December 15, 2020, 6:15pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/11 "2020-12-15T18:15:53Z")

</div>

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](https://teacher.desmos.com/activitybuilder/custom/5fd0d5dcd3ec130cf452a191)

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?

---

<div class="post-metadata">

### Author: ![Daniel\_Grubbs](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/daniel_grubbs/32/719_2.png) [@Daniel\_Grubbs](https://cl.desmos.com/u/Daniel_Grubbs)
#### Post date: [December 15, 2020, 8:32pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/12 "2020-12-15T20:32:27Z")

</div>

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.

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

```

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

---

<div class="post-metadata">

### Author: ![Charlie\_Cannon](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/charlie_cannon/32/1708_2.png) [@Charlie\_Cannon](https://cl.desmos.com/u/Charlie_Cannon)
#### Post date: [December 16, 2020, 2:23pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/13 "2020-12-16T14:23:07Z")

</div>

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. 🙂

---

<div class="post-metadata">

### Author: ![JayChow](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/jaychow/32/1674_2.png) [@JayChow](https://cl.desmos.com/u/JayChow)
#### Post date: [December 16, 2020, 4:39pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/14 "2020-12-16T16:39:36Z")

</div>

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.

> **["Breaking" numericValue • Activity Builder by Desmos](https://teacher.desmos.com/activitybuilder/custom/5fda360a35d39f0bb7fc131d)**
>
> by Jay Chow

---

<div class="post-metadata">

### Author: ![Charlie\_Cannon](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/charlie_cannon/32/1708_2.png) [@Charlie\_Cannon](https://cl.desmos.com/u/Charlie_Cannon)
#### Post date: [December 16, 2020, 5:10pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/15 "2020-12-16T17:10:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JayChow](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/jaychow/32/1674_2.png) [@JayChow](https://cl.desmos.com/u/JayChow)
#### Post date: [December 16, 2020, 5:14pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/16 "2020-12-16T17:14:38Z")

</div>

strange! I re-published, try again?

---

<div class="post-metadata">

### Author: ![Charlie\_Cannon](https://yyz1.discourse-cdn.com/flex031/user_avatar/cl.desmos.com/charlie_cannon/32/1708_2.png) [@Charlie\_Cannon](https://cl.desmos.com/u/Charlie_Cannon)
#### Post date: [December 16, 2020, 5:16pm UTC](https://cl.desmos.com/t/rounding-truncating/1035/17 "2020-12-16T17:16:08Z")

</div>

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