rounding up numbers

NBCC
2017-05-15
2021-04-08
  • NBCC - 2017-05-15

    Hi all,

    Probably a stupid question but i have some troubles with rounding up numbers. So just like 3/2 = 2
    It Always has to be rounded to the biggest number.
    Any suggestions here? (i don't use the oscat library)

    Many thanks.

     
  • ekmekci - 2017-05-15

    So what do you want ?
    If you want to be see 3/2 = 1.5
    you have to define LREAL variable.

    For example,

    variable1 : LREAL;

    variable1 := 3/2;

    so, you can see variable1 = 1.5

     
  • Anonymous - 2017-05-15

    Originally created by: scott_cunningham

    There are many ways to always round up, but here is one:

    RVal = REAL(3)/2;
    IVal = INT(3/2);
    If RVal > INT_TO_REAL(IVal) THEN
    Β  Β IVal := IVal + 1;
    END_IF
    
     
  • NBCC - 2017-05-15

    scott_cunningham hat geschrieben:
    There are many ways to always round up, but here is one:

    RVal = REAL(3)/2;
    IVal = INT(3/2);
    If RVal > INT_TO_REAL(IVal) THEN
    Β  Β IVal := IVal + 1;
    END_IF
    

    Thank you. I tried it and it worked

     
  • josepmariarams - 2017-10-14

    a/b+((a div b)+(b-1))/b

     
  • laruso - 2021-04-08

    My proposal for integer values:

    VAR
        a: INT;   // Dividend
        b: INT;   // Devisor
        c: INT;   // Result
    END_VAR
    
    -----
    
    c := (a-1)/b+1;
    

    I needed that conversation for getting the correct number of column and row, out of the (overall) count of an array (to correct the code of OSCAT's "CSV_PARSER_FILE_DEMO"). It works with the integrated truncation feature while calculating with integer values. Or you look after "ROUND" functions in OSCAT's library.

     

Log in to post a comment.