Page 1 of 1

How to round 2 digits after comma ?

Posted: 15 Nov 2009 19:26
by crocu
Hello


I'm using "float to string" to have a calculation displayable on a LCD, this gets a value like this : 12.12345 ( the number is 5 digits after comma )
FloatToStr(calculation, calc);
I would like to get the value rounded to 2 digits only after comm in order to get something like this : 12.12

Can you show me how to do this ?

Many thanks for your help,

Posted: 16 Nov 2009 00:25
by MARIO
See strncpy() function in the help file... (ANSI C String library)

BR.

Re: How to round 2 digits after comma ?

Posted: 30 Mar 2010 01:17
by Maher-dj
please help
i'm using "FloatTOStr" but the compilation don't work "Unresolved extern 'strcpy'".
i would like to read a value from the ADC ,convert it to String ,and displayed on LCD.
I would like to get the value rounded to 2 digits only after comma.
thanks. :)

Re: How to round 2 digits after comma ?

Posted: 08 Apr 2010 10:36
by tihomir.losic
Hello,

in installation folder of mikroC you have examples for some of microcontrollers, and for PIC16F877A,
you have example ADC on LCD.
So, look at this example, this can help you.

Also, my recommendation is to install mikroC PRO for PIC
mikroC PRO for PIC 2009 includes a set of libraries and examples intended to facilitate application development.
Routines are documented in detail and allow quick start in programming microcontrollers.

Best regards,

Losic Tihomir

Re: How to round 2 digits after comma ?

Posted: 08 Apr 2010 11:09
by crocu
Thanks, but my code has been written with Mikro C , will it fully compatible with Mikro C Pro version ?

i don't want to spend long time to port it to Pro version ...

Please let me know,

Re: How to round 2 digits after comma ?

Posted: 08 Apr 2010 11:53
by tihomir.losic
Hello,

please, try this code (mikroC, PIC16F877A):

Code: Select all

unsigned char ch;
unsigned int adc_rd;
char *text;
long tlong;

void main() {
  INTCON = 0;                              // disable all interrupts
  Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete
  LCD_Cmd(LCD_CURSOR_OFF);                 // send command to LCD (cursor off)
  LCD_Cmd(LCD_CLEAR);                      // send command  to LCD (clear LCD)

  text = "mikroElektronika";               // assign text to string
  LCD_Out(1,1,text);                       // print string a on LCD, 1st row, 1st column
  text = "LCD example";                    // assign text to string
  LCD_Out(2,1,text);                       // print string a on LCD, 2nd row, 1st column

  ADCON1     = 0x82;                       // configure VDD as Vref, and analog channels
  TRISA      = 0xFF;                       // designate PORTA as input
  Delay_ms(2000);
  text  = "voltage:";                      // assign text to string
  while (1) {
    adc_rd  = ADC_read(2);                 // get ADC value from 2nd channel
    LCD_Out(2,1,text);                     // print string a on LCD, 2nd row, 1st column

    tlong = (long)adc_rd * 5000;           // covert adc reading to milivolts
    tlong = tlong / 1023;                  // 0..1023 -> 0-5000mV

    ch     = tlong / 1000;                 // extract volts digit
    LCD_Chr(2,9,48+ch);                    // write ASCII digit at 2nd row, 9th column
    LCD_Chr_CP('.');

    ch    = (tlong / 100) % 10;            // extract 0.1 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch    = (tlong / 10) % 10;             // extract 0.01 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point

    ch    = tlong % 10;                    // extract 0.001 volts digit
    LCD_Chr_CP(48+ch);                     // write ASCII digit at cursor point
    LCD_Chr_CP('V');

    Delay_ms(1);
  }
}//~!
Best regards,

Losic Tihomir