How to put a comma ( , ) on 1,000 printing on 16x2 LCD...??

Generally when a value 1000 is printed the print looks 1000 to a HD44780

But I want to put a comma after thousand unit and want to print it as 1,000 or 11,000

how to do this..??

something like this...

int temp = 12345;

if (temp > 1000) 
{
  lcd.print(temp/1000);
  lcdprint(",");
}
lcdprint(temp%1000);

robtillaart:
something like this...

int temp = 12345;

if (temp > 1000)
{
  lcd.print(temp/1000);
  lcdprint(",");
}
lcdprint(temp%1000);

Thankyou robtillaart for replying..

I tried your way but it didnt work properly...

Strange,

Can you post you sketch and the output it generates, and the output you expect?

See I wrote this

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int potpin = 0;
int temp = 12345; 

void setup() {
  lcd.begin(16, 2);
}

void loop() 
{ 
  temp = analogRead(potpin);
  temp = map(temp, 0, 1023, 0, 2000);

lcd.setCursor(0, 0);
   

     if (temp > 1000) 
     {
        lcd.print(temp/1000);
        lcd.print(",");
      }
      
      lcd.print(temp%1000);
      lcd.print(" ");
}

see.. I want to print the pot value to the lcd. when the reading reaches 999 and as it steps to 1000 there should be a comma after 1 ( so I want it to print as 1,000 )

if the reading is 1530 I want it to print as 1,530..

I'm also playing around with a 16x2. It looks to me as if that code really should print 1,001 for the value 1001. It does the wrong thing for exactly the value 1000, though, and probably the wrong thing for any negative value (haven't verified it).

Here's an alternative version:

// Does not work right for exactly the value -32768!
void printWithComma(int val)
{
    if (val < 0) {
        val = -val;
        lcd.print("-");
    }
    if (val >= 1000) {
        int v = val / 1000;
        lcd.print(v);
        lcd.print(",");
        val = val - (v * 1000);
        char buf[4];
        sprintf(buf, "%03d", val);
        lcd.print(buf);
    }
    else {
        lcd.print(val);
    }
}

It assumes there's a global named "lcd" to do the printing. Just paste this in your sketch after you declare the lcd, and before your setup or loop function, and call it to print the value -- it should work just like you want it for all 16-bit integer values except -32768.

I wrote this..

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int val;
int potpin = 0;

void setup() {
  lcd.begin(16, 2);
}


void loop(){
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 2000);
  
  lcd.setCursor(0,0); 
  
      if (val < 0) {
        val = -val;
        lcd.print("-");
    }
    if (val >= 1000) {
        int v = val / 1000;
        lcd.print(v);
        lcd.print(",");
        val = val - (v * 1000);
    }
    
  lcd.print(val);
  lcd.print(" ");
}

With this code after the value 999 when the value gets to 1000 the lcd prints 1,0 where as I want it to print 1,000

Once you get a value below 1000, to print after the decimal point, use sprintf to populate an array with a 3 digit value:

char stg[4];
sprintf(stg, "%03d", val);

Then, send stg to the lcd.

The %03d format specifier says to output the value as an int (the d) with 3 places (the 3), with leading 0s as required (not to hard to figure out what part causes that).

PaulS:
Once you get a value below 1000, to print after the decimal point, use sprintf to populate an array with a 3 digit value:

char stg[4];
sprintf(stg, "%03d", val);

Then, send stg to the lcd.

The %03d format specifier says to output the value as an int (the d) with 3 places (the 3), with leading 0s as required (not to hard to figure out what part causes that).

Thank you PaulS for helping..

I could not understand you..
Can you please show me in the code...??

    if (val >= 1000) {
        int v = val / 1000;
        lcd.print(v);
        lcd.print(",");
        val = val - (v * 1000);

        // Generate a string with exactly 3 characters representing val
        char stg[4];
        sprintf(stg, "%03d", val);

        // Print the string
        lcd.print(stg);
    }

Note that you should have an else block to handle the case where val < 1000.