Start a new topic

Doing Math and Displaying as Decimal (float)?

Hello,


I am trying to figure out how to do math (division in my case), and display that answer as a decimal in a text box. I thought I could be sneaky and convert a number value (int) into a text box (string), but I have already lost the decimal values at that point. The code I tried was:


va0.val=4000/h0.val

cov va0.val,t0.txt,0


I have a value that I set with a slider. I then want to take 4000 divided by that value, and display it as a decimal number, not an integer. My slider will go from 1 to 2047.


Is there anyway to do this within the Nextion editor. I'm know I can send the value out to an arduino, have it do the math and send back a decimal string, but I don't want to do that if Nextion can do it.


Thanks!


I haven't seen any float capabilities. division provides whole number division on STM32F.

60000/20001=2




Not to be a replacement for float, but you could multiply 4000*factor before division, then incrementally calc

va2=4000*1000

va3=va2/2047

then move your decimal point over three.  I haven't seen string manipulation either.


@Patrick I was actually doing that before I posted this thread. I then realized that I needed some string commands, so I went looking, but couldn't find any. I was hoping there was an easier way. I'll dig around some more, and post what I come up with tomorrow. It sounds like it's gonna end up being a sort of hack though. Truly appreciate the quick response. This forum is incredible! I have gotten practically immediate responses on my threads. Amazing! Such a great environment to learn!

[SOLVED] "sort of"


In the above posts Patrick had a great idea to get around the lack of float math operations by using large integer values to capture the decimal places, then just shift the decimal point accordingly. Unfortunately, the Nextion Editor also lacks, as far as I can tell, any string manipulation commands. So In order for this to work, it would all have to be done by hand.


The way I solved this issue was as follows:

va0.val=4000/h0.val             <<capture value before decimal

va1.val=40000000/h0.val      <<capture 4 decimal values (add more zeros for more decimals

va2.val=va0.val*10000         <<multiply original value by multiplier in line 2 to isolate decimal

va3.val=va1.val-va2.val        <<determine the decimal value

cov va0.val,va4.txt,0            <<save value before decimal as text

cov va3.val,va5.txt,4            <<save decimal as text

t0.txt=va4.txt+"."+va5.txt      <<display whole value as text


This seems to work well for me. It certainly isn't clean, but it gets the job done. I would leave to here any other ideas on how to accomplish this. Hope this helps somebody!


2 people like this
Login or Signup to post a comment