Rounding values

devilblack 5 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 Tananaev 5 years ago

You can use Math.round or something similar.

devilblack 5 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 < ...)
Josue 5 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 Tananaev 5 years ago

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

Hristo 5 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

Raoul 2 years 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

ppet 2 years ago

I would be very interested too

Raoul 2 years 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

ppet 2 years 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 Tananaev 2 years ago

You probably missed the multiplication.

ppet 2 years 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)

ppet 2 years 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 Tananaev 2 years ago

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