Number formatting of result in equations

I have simplified my code below, but I have a problem with number formatting I think due to the types I have used.

The number shown on the mobile app has 3 decimal points but they are always #.000

Is this because I'm dividing a unsigned long by a float so the output is an unsigned long?

//timer define variables
int timerexceed = 1; // define when timercounter exceeds timerduration to a value of 1, 0 when the
duration has not been exceeded
unsigned long startoftimer = millis (); // time for reference for counter
unsigned long currenttime = millis (); // current time in milliseconds
unsigned long timerduration = 21600000; // duration in millseconds to count to
float timerdurationhour;
unsigned long timercounter = 0; // time since timer reset in millseconds

// setup
void durationUpFunc() // send duration to mobile device but convert to hours
timerduration = timerduration + 600000;
timerdurationhour = timerduration/3600000;
Blynk.virtualWrite(V22, timerdurationhour);

if (pedalButtonState==1) // reset timer when button is pushed

// loop

void loop ()
timercounter = currenttime-startoftimer;
if (timercounter > timerduration) // check if the duration has been exceeded
timerexceed = 1;
digitalWrite(timerexceedpin, HIGH);

Timerdurationhour will be an integer, because it is the result of an integer divide.

To fix this, append a decimal point to the constant 3600000, or cast timerduration as a float in the divide.

That sounds easy. I can't upload my code for a day to verify (thus why this topic is opened a little prematurely), so just checking I understood this right.

If I change the equation to the following, it will output timerduration as a float variable?

timerdurationhour = timerduration/3600000.00 ;


just change the declaring of "unsigned long timerduration" to "float timerduration" at the top of my code with no need to add the decimal points to the 3600000

Next question is (another premature one). If I leave the timerdurationhour calculating as an integer (always rounds down, or drops the decimal points yes) and then do the following calculation, will this output the timer in hours and minutes (but its actually a decimal or floating that goes from 1.59 to 2.00 (so is a decimal but behaves like time)? Or will this output an integer as timerdurationhour is an integer?

Does mathematical equations always go to the lowest decimal point format or to the format of what it is getting divided by? Please feel free to reply "see this link" or "search more"

float timerdurationminute;
float timerdurationsend;
int timerdurationhour;

timerdurationminute = (timerduration/3600000.00) - timerdurationhour;

timerdurationsend = timerdurationhour + timerdurationminute

change the declaring of "unsigned long timerduration" to "float timerduration"

will cause other problems. Stick with unsigned long integers.

To calculate hours and minutes separately without losing precision, use unsigned integers for all calculations. Example, assuming "unsigned long timerduration" is the total elapsed time in milliseconds:

unsigned int hours = timerduration/3600000UL;  //max value of hours = 65535
unsigned int minutes = (timerduration - 3600000UL*hours)/60000UL;

The rules for arithmetic operations is C/C++ are somewhat complicated and unless you study them carefully, it is best not to guess, and to code all operations explicitly so that they are clear to you and others.

Note: the "UL" in the constants above is not necessary in this case, but is included for clarity.


My code is like below now so hope I have it right....

  timerduration = timerduration - 600000;
  unsigned int hours = timerduration/3600000;  //max value of hours = 65535
  unsigned int minutes = (timerduration - 3600000*hours)/60000;
  Blynk.virtualWrite(V22, hours);
  Blynk.virtualWrite(V23, minutes);