How to round an decimal value Grafana
Asked Answered
F

2

10

Is there a way to round a decimal value in grafana? round() and ceil() functions gets an "instant-vector", not a numeric value, and for example, adding a query like ceil(1/15) will return 0.

Felipe answered 1/6, 2018 at 0:10 Comment(1)
Edit: Datasource is prometheus. The example is based on this documentationFelipe
M
7

It depends what you're using to display the data, for example a single stat or gauge you'll find the 'Decimals' option in Grafana, for graphs it's in the 'Axes' options. You don't need to do this in the query for the metric.

Midlands answered 1/10, 2018 at 11:43 Comment(3)
Yes, but i need it in a query, not as a transformation of the result. e.g: sum(some_metric{property="some_value"}) + ceil($param / 15) Is this possible?Felipe
It should yeah, can you post the actual error you're getting? From your OP it sounds like it was saying something like " expected type instant vector in call to function "round", got scalar" is that true? If so to test you could wrap it in vector: EG in the Prometheus console ceil(1/15) will fail, but ceil(vector(1)/15) will return 1, round 0Midlands
That's the answer @andy!. I was missing the vector(<esclar>) part.Felipe
A
4

PromQL provides round() function, which can be used for rounding query results to the nearest multiple of the second arg. For example, round(q, 1) rounds q results to the nearest integer, while round(q, 0.1) rounds q results to the nearest multiple of 0.1. Prometheus doesn't allow passing plain scalars (aka numeric constants) into round() function. This can be solved by wrapping the numeric constant into vector() function. For example, the following query returns 0.3:

round(vector(1/3), 0.1)
Armin answered 29/3, 2022 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.