Error trying to use Math.* in inline statements

What I’m trying to do

I’m trying to use javascript’s Math.trunc() function in an inline statement.

Things I have tried

=Math.trunc((date(today) - this.birth.date).years)

It throws:

Dataview (for inline query '=Math.trunc((date(today) - this.birth.date).years)'): Cannot call type 'null' as a function. 

So it seems somehow Math isn’t imported in inline statements?

First of all :slight_smile: :

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks) is properly shown.

There is a difference between `= … `, and `$= … `. The first variant is an inline variant for a single value in the DQL query format/field, allowing you to do `= this.birth.date`. And the other is the inline variant of dataviewjs, allowing you to do stuff like `$= dv.span(dv.current())`.

To have access to something like Math.trunc() you would use the second variant, unless there is a matching DQL query function found in this list.

The error message you list though, is related to Math.trunc() not being defined in the inline DQL query context. There is a round() defined though, but in order to eliminate the rounding effect, you could do something like:

`= round((date(today) - this.birth.date).years - 0.5)`

An alternative way to get the number of years is to do:

`= date(today).year - this.birth.date.year`

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.