# Convert var to double

**URL:** <https://discourse.mc-stan.org/t/convert-var-to-double/314>\
**Category:** Developers\
**Created:** [March 24, 2017, 2:03pm UTC](https://discourse.mc-stan.org/t/convert-var-to-double/314 "2017-03-24T14:03:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [March 24, 2017, 2:03pm UTC](https://discourse.mc-stan.org/t/convert-var-to-double/314/1 "2017-03-24T14:03:00Z")

</div>

I need to do something like this for a check function in a way that works for `a1` being either double or var:

```
int num_terms;
num_terms = floor(fabs(a1));

```

Is there some standard function for this?

Krzysztof

---

<div class="post-metadata">

**Author:** ![sakrejda](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/sakrejda/32/845_2.png) [@sakrejda](https://discourse.mc-stan.org/u/sakrejda)\
**Post date:** [March 24, 2017, 2:28pm UTC](https://discourse.mc-stan.org/t/convert-var-to-double/314/2 "2017-03-24T14:28:38Z")

</div>

Of course I just needed to ask the question to figure it out, thanks, in reference to xkcd #979:

```
stan/math/prim/scal/fun/value_of_rec.hpp

```

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [March 24, 2017, 10:26pm UTC](https://discourse.mc-stan.org/t/convert-var-to-double/314/3 "2017-03-24T22:26:36Z")

</div>

You should just be able to use `value_of` rather than `value_of_rec`, I think. The function `floor` returns a `double`, but you can cast it if you want to be explicit or just drop the cast. And you always want to declare-define where possible, so this should be:

```
int num_terms = static_cast<int>(floor(fabs(value_of(a1))));

```

assuming all those functions are in namespace.
