Iostream problems

I am trying to remove decimals out of my reading. The compiler does not like #include <iostream> so is there a better way to accomplish this? Sample of what I’m trying to accomplish is below. The LCD is showing ‘100.00’ but I want it as ‘100.’ Thanks

void loop() {
    static char buttons;
    static char curState = 0;
    static char nxState = 0;
    double distance = readDistance(); //Distance is in millimeters
    double percent = (1-((distance - levels.full)/(levels.add - levels.full)))*105;
    cout<< percent<< end1;
    cout<< setprecision(1)<<percent<< end1;

Well you could use printf instead, or perhaps if you don’t need the precision just make percent an integer …?

Hi @Allan

I am not clear where you think cout is going on this platform? The USB Serial stream?

Anyway the solution is to use floor() or round() to control your results something like this:

percent = round(percent*10.0)/10.0;  /* change 72.77 to 72.8 */
1 Like

Sorry, I am using a LCD screen with i2c communication.
The "percent = round(percent10.0)/10.0;" (fyi: I changed it to 'percent = round(percent1)/1;’) works great thank you! However I would like to not display “.00” on the LCD. Is that possible? Thanks

Also I’ve tried to use setprecision which will compile but do nothing.

If you want full control, I recommend using something like this:

char str[64];
snprintf(str,64,"The percentage is %2.0d%%",percent);
//send the char array to your display here

Because % is a format directive, you need two %% to print one.

Thanks for every ones help. This is what ended up working.

lcd.printf(“Level: %.0f”, percent); //no decimals
lcd.print("% ");