Numbers in Print function

Hi to all friends

I would like to ask a question about printing numbers in my screen or in my serial monitor

lets say that i have a potentiometer that gives me results from ''500'' until ''7000'' with map function

so i can take lot of variable numbers... like 522, 545, 1123, 1476 etc

is any formula or special code that i could use in order to print these numbers as a ''hundreds'' (can't remember the right word in english)

lets say that if i am getting result ''534'' the serial print would be ''500''

756 to 700

1345 to 1300

etc..

thanks in advance

You should set out your requirements exactly. Example: 700 thru 799 >>>——-> 700 ? 1300 thru 1399 >>>——-> 1300 ?

That's not a programming question. That's just math.

int i = 543;

int trimmed =  i - (i %100);

Serial.print(trimmed);   // prints 500

Thanks for the reply :slight_smile:

i am using the code below to print the results from the variable ‘‘rpm’’

#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC


//////////////////////////////////////

float value = 0;
float rev = 0;
int rpm;
int oldtime = 0;
int time;


void isr() //interrupt service routine
{
  rev++;
}

void setup()
{
  Serial.begin(9600);

  /////////////////////////////////////////

  // assign default color value
  if (u8g.getMode() == U8G_MODE_R3G3B2)
  {
    u8g.setColorIndex(255);     // white
  }
  else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
  {
    u8g.setColorIndex(3);         // max intensity
  }
  else if (u8g.getMode() == U8G_MODE_BW)
  {
    u8g.setColorIndex(1);         // pixel on
  }
  else if (u8g.getMode() == U8G_MODE_HICOLOR)
  {
    u8g.setHiColorByRGB(255, 255, 255);
  }
  ////////////////////////////////////////////

  attachInterrupt(0, isr, RISING); //attaching the interrupt

}

void loop()
{
  delay(100);
  detachInterrupt(0);           //detaches the interrupt
  time = millis() - oldtime;    //finds the time
  rpm = (rev / time) * 60000 / 2; //calculates rpm
  oldtime = millis();           //saves the current time
  rev = 0;

  Serial.println(rpm, DEC);
  //lcd.print(" RPM");

  attachInterrupt(0, isr, RISING);


  u8g.firstPage();
  do {
    Rpms();   //mpainei to onoma tou void me to display
  } while ( u8g.nextPage() );

}


//////////////////////////////////////////////RPM//////////////////////////////
void Rpms(void) {
  u8g.setFont(u8g_font_fub11);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
  u8g.drawStr(4, 20, "RPM");
  u8g.setPrintPos(70, 20);
  u8g.print(rpm);
  //delay(200);
}
////////////////////////////////////////////////////////////////////////////////////////////

so i cant do that with just a simple line code…

i have to use the ‘’ if ‘’ statement ?

example :

  if (rpm >= 500 || rpm <= 599 ) {
    Serial.println("500")
  }
  else if (rpm >= 600 || rpm <= 699 ) {
    Serial.println("600")
  }
  else if (rpm >= 700 || rpm <= 799 ) {
    Serial.println("700")
  }
  else if (rpm >= 800 || rpm <= 899 ) {
    Serial.println("800")
  }

Right ?

Did you try Delta_G’s example ?

Ask yourself; what will happen?
if (rpm >= 500 || rpm <= 599 )

What does && do?

Delta_G: That's not a programming question. That's just math.

int i = 543;

int trimmed =  i - (i %100);

Serial.print(trimmed);   // prints 500

Thanks ... i am still new in coding.. and lot of times i need a guide to take the right path :) but with practice i am starting to learn from my mistakes

Thank you both for the replies :)

Yes i tried what ''Delta_G'' suggested , and worked fine :)

Thanks again :)