How to round 2 digits after comma ?

General discussion on mikroC.
Post Reply
Author
Message
crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

How to round 2 digits after comma ?

#1 Post by crocu » 15 Nov 2009 19:26

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,

MARIO
Posts: 978
Joined: 18 Aug 2008 22:13
Location: Brasil

#2 Post by MARIO » 16 Nov 2009 00:25

See strncpy() function in the help file... (ANSI C String library)

BR.

Maher-dj
Posts: 1
Joined: 30 Mar 2010 01:08

Re: How to round 2 digits after comma ?

#3 Post by Maher-dj » 30 Mar 2010 01:17

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. :)

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: How to round 2 digits after comma ?

#4 Post by tihomir.losic » 08 Apr 2010 10:36

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
mikroElektronika [Support team]

crocu
Posts: 71
Joined: 18 Jun 2008 09:28
Location: France, Macon

Re: How to round 2 digits after comma ?

#5 Post by crocu » 08 Apr 2010 11:09

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,

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: How to round 2 digits after comma ?

#6 Post by tihomir.losic » 08 Apr 2010 11:53

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
mikroElektronika [Support team]

Post Reply

Return to “mikroC General”