Unofficial Nextion/TJC User Forum

Nextion mathematical operations

Hi. Can someone please assist me on how to perform mathematical operations in nextion (which involves decimals as well).

I have a screen with some numbers and 4 radio buttons, and then a button to calculate the a result from a formula, then use the result on another screen. The numbers are:
QNT.val (a value, non-decimal, from another screen)
BaseSize.val (value from one of the radio buttons selected, 100%, 75%, 50% or 25%)…here are the 1st decimal answer issue…
SingleSize.val (a value, non-decimal, from another screen)

My formula is:

Result.val=QNT.valBaseSize.val/SingleSize.val56%

In a “normal” maths operation, it will be:
Result.val=(QNT.valBaseSize.val) / (SingleSize.val0.56)

I have tried many options ect but cannot get it to work/compute. Seems like the decimals are the issue? Any ideas to get this computed?

Thanks in advance

As documented in the Nextion Instruction Set on the Nextion.tech website, only integer operations work and are permitted in Nextion language. You will have to extend your fractions by 10^n to get a reasonable precision.

First, avoid the second division. Each integer division kills precision since the remainder is not taken into account. Rewrite your fraction following the rule that a / (b / c) = (a * c) / b.

Now, to come around the c = 0.56 problem, you extend numerator and denominator by 100: (a * 100 * 0.56) / b * 100 which you can rewrite as (a * 56) / (b * 100) which gives you a precise integer result, but (logically) without decimals.

If you want decimals, for example 2, the only way is to compute the result extended by 10^2 which gives you still an integer which is shifted by two positions to the left, while the 2 rightmost digits are the desired decimals. The formula simplifies then to (56 * a) / b

There is a Float component to display such extended integers as pseudo floats. You might now set the .val of the Float component to your result, set the corresponding attribute to 2 decimals and you are done.

2 Likes

This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).