Rounding values

devilblack3 years ago

Hi, Is there an easy way to round output values ?
I've currently got a value as "4.071" but would like to display "4.1" instead.
Thanks

Anton Tananaev3 years ago

You can use Math.round or something similar.

devilblack3 years ago

I tried Math.round(battery) but got an Invalid parameter/constraint violation :

org.traccar.handler.ComputedAttributesHandler.computeAttribute@102![5,19]: 'Math.round(battery);' attempting to call method on null - JexlException (... < ComputedAttributesHandler:102 < AttributeResource:57 < ...)
Josue3 years ago

Traccar in computed attributes is not recognizing any java or js functions (math, parsefloat, parseint, tofixed)

org.traccar.handler.ComputedAttributesHandler.computeAttribute@102![5,17]: 'Math.floor(temp1);' attempting to call method on null - JexlException (... < ComputedAttributesHandler:102 < AttributeResource:57 < ...)

Anton Tananaev3 years ago

You need an instance of an object to call a method. That's why I said "something like".

Hristo3 years ago

Hey guys,
Would anyone be willing to share the code for creating an instance of an object in Traccar Computed Attributes in order to use methods?

I would like to do something like this:

Vehicle Battery
power ?
power.toFixed(2) + " V" :
null
Number

Many thanks,
Hristo

Raoul5 months ago

Hi Hristo,
Did you already find a solution for your Math.round function? I would like to round of my power (battery voltage) to two decimals.
I would very much appreciate if you could help me in this matter.
Kind regards, Raoul

ppet4 months ago

I would be very interested too

Raoul4 months ago

I have used math:round(power*100.00)/100.00
which gives a 1 decimal round of.
Do not forget to conect the Computed Attribute to the device and to wait for new data before the effect is visible.
I used the same formula for the temperature:
math:round(temp1*100.00)/100.00

ppet4 months ago

Thank you Raoul for the reply, but unfortunately when i use the formula you give i get the following

org.traccar.handler.ComputedAttributesHandler.computeAttribute:119@1:11 unsolvable function/method 'round(Object)' - Method (ComputedAttributesHandler:120 < AttributeResource:63 < ... < OverrideFilter:50 < ...)

the formula only without anything else in the expresion

Anton Tananaev4 months ago

You probably missed the multiplication.

ppet4 months ago

i tried a number of ways before posting
the error message i syubmitted is by copy pase of the formula only Raoul posted in a new computed attribute

this formula works for you Anton?

(i have alot of teltonika devices that report a number of values with 15 decimals randomly like 11.325000000000001v)

ppet4 months ago

OK, it works

the problem was that the email i received with Raoul answer, the asterisk was omitted
i copy paste the formula from the email

"Raoul wrote:
I have used math:round(power100.00)/100.00
which gives a 1 decimal round of.
Do not forget to conect the Computed Attribute to the device and to wait for new data before the effect is visible.
I used the same formula for the temperature:
math:round(temp1100.00)/100.00
Post Link: https://www.traccar.org/forums/topic/rounding-values/#post-93042"

sorry for the trouble

Anton Tananaev4 months ago

That's because Raoul didn't format the markdown properly. I already fixed it in his comment.