modulo (%) operator results

Hello,

I am reading pH values from an analog input and for the integer side division by 100 works well.
For the fractional side, modulo (%) operator works well for results greater than 10, meaning 7.11 gets displayed the right way, but when the fractional is less than 10, like 6, instead of 7.06 I get 7.6 ???

so analog reading 711
PH value integer side; 711/100 ->7
fractional; 711%100 ->11
display; 7.11

analog reading 709
integer; 709/100 ->7
fractional; 709%100 ->9
display; 7.9 !!!

How can I fix this ?
Thank you very much,
Marian

PS. I can go with… if fractional <10 …, but I guess there is a more intelligent way of dealing with this situation.

fractional; 709%100 ->9
display; 7.9 !!!!

How can I fix this ?

Make sure you print leading zeros.

How/where are you displaying the value? If it is using Print::print() (directly, or through inheritance), the print method for floats now allows specifying the number of decimal places to show, so it may not be necessary to emulate this anymore.

I have the pH value displayed on an LCD and on this I have a small if loop checking for fractional < 10 and if true print a leading zero since on the LCD I can control the position.

My problem is on the webpage where where I can not control the position. Paul can you please detail how whould you use the print instruction for this ?

Thank you,
Marian

My problem is on the webpage where where I can not control the position.Paul can you please detail how whould you use the print instruction for this ?

I'd need to see your code for getting data for the web page to display.

If you are using the Client/Server classes in the Ethernet library, they both derive from Print, so you should just be able to have something like:

float pH = 7.043;
client.print("pH = ");
client.println(pH, 4); // print 4 decimal places

Thank you Paul.

Here is the code I use:

client.println(""); client.print(ph_val / 100); client.println("."); client.print(ph_val % 100); client.println("");

Can it be optimized ? ph_val is the value provided by the analog read.

cheers,
Marian

Applying Paul’s comment to your code gives

float ph = ph_val  / 100.0 ;  // makes a float with 2 digits
client.println("<td align=center>"); client.print(ph, 2); client.println("</td>");