Skip to content

Align Right? #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lain-d opened this issue Mar 31, 2018 · 5 comments
Open

Align Right? #22

lain-d opened this issue Mar 31, 2018 · 5 comments
Assignees

Comments

@lain-d
Copy link

lain-d commented Mar 31, 2018

Any way to make the digits align right instead of left when they are shorter than 4 digits?

@bremme
Copy link
Owner

bremme commented Apr 9, 2018

This sounds like a good option, have to think about a proper solution. The library just implements the Arduino print/write methods and these don't support padding or right alight. I should probably just add a custom method printNumber with a align argument. In the mean time you can just use a custom function yourself, quick and dirty example:

void printAlignRight(int number) {
  if (number < 10) {
    display.setCursor(0, 3);
  }
  else if ( number < 100) {
    display.setCursor(0, 2);
  }
  else  if (number < 1000) {
    display.setCursor(0, 1);
  } else {
    display.setCursor(0, 0);
  }
  display.print(number);
}

Hope this helps.

@bremme bremme self-assigned this Apr 9, 2018
@Bilick88
Copy link

Bilick88 commented Sep 11, 2018

Let me "improve" the example that @bremme has showed:

void printRight(int number) {
  if(number>9999) {
    display.print("----");
  }
  else {
  if (number < 10) {
    display.setCursor(0,0);
    display.print(" ");
    display.setCursor(0,1);
    display.print(" ");
    display.setCursor(0,2);
    display.print(" ");
    display.setCursor(0,3);
  }
  else if ( number < 100) {
    display.setCursor(0,0);
    display.print(" ");
    display.setCursor(0,1);
    display.print(" ");
    display.setCursor(0,2);
  }
  else  if (number < 1000) {
    display.setCursor(0,0);
    display.print(" ");
    display.setCursor(0, 1);
  } else {
    display.setCursor(0, 0);
  }
  display.print(number);
  }
}

Hope it helps :)

@lain-d
Copy link
Author

lain-d commented Dec 19, 2018

Hey sorry to take so long to join in. Thanks for addressing this. I came up with a similar solution to Bilick88's. Basically adding in space padding if the digits are shorter than a certain length.

I'm not really very good at C optimization, so I'm not sure what is faster. But in my case I casted the int to a string and added spaces in a (While len < 4) loop.

@jmlcatarino
Copy link

Hi there, has anyone got a solution for this issue? I've tested the above solutions and it changes nothing to my sketch.
I'm trying to create a parts counter but with this thing on the left it just doesn't look right.

@jasonacox
Copy link

jasonacox commented Jun 14, 2020

Thanks for this great library, @bremme ! I needed a right align number print function as well. However, the example functions above do not handle negative values. I made a few minor tweaks:

void printNum(int number) {
  bool negative = false;

  display.clear();
  if (number < 0) {
    number = -number;
    if (number >= 1000) {
      display.print("----");
      return;
    }
    negative = true;
  }
  if (number > 9999) {
    display.print("----");
  }
  else {
    if (number < 10) {
      display.setCursor(0, 3);
    }
    else if ( number < 100) {
      display.setCursor(0, 2);
    }
    else  if (number < 1000) {
      display.setCursor(0, 1);
    } else {
      display.setCursor(0, 0);
    }
    display.print(number);
  }
  if (negative) {
    display.setCursor(0, 0);
    display.print("-");
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants