Thread: Abs function

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    42
    Rep Power
    2

    Abs function


    Hi there.. I am trying to add this row, the abs function works, but I need to cut the decimals down to none..

    how do I do this?

    Code:
    abs(float(row['DEAL_MARGIN']) / float(row['COUNTER_AMT']) * 10000))
    complete code for adding the row:
    Code:
    t.addData((row_nr,11),abs(float(row['DEAL_MARGIN']) / float(row['COUNTER_AMT']) * 10000))
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    You may try the int function (though it is intended for a different purpose)

    Code:
    >>> int(abs(-2.345))
    2
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    Code:
    >>> help(round)
    Help on built-in function round in module __builtin__:
    
    round(...)
        round(number[, ndigits]) -> floating point number
        
        Round a number to a given precision in decimal digits (default 0 digits).
        This always returns a floating point number.  Precision may be negative.

    Comments on this post

    • ChristopherL agrees : Thank you :)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    42
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    >>> help(round)
    Help on built-in function round in module __builtin__:
    
    round(...)
        round(number[, ndigits]) -> floating point number
        
        Round a number to a given precision in decimal digits (default 0 digits).
        This always returns a floating point number.  Precision may be negative.
    Had some trouble with it so thought it was something, just needed to sort out my paranthesis

    Solved with

    Code:
    t.addData((row_nr,11),round(abs(float(row['DEAL_MARGIN']) / float(row['COUNTER_AMT']) * 10000),0))
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by ChristopherL
    I need to cut the decimals down to none
    Remember that round does more than just cutting down the decimals
    Code:
    >>> round(2.35)
    2
    >>> round(2.53)
    3

    Comments on this post

    • ChristopherL agrees : Thank you :)

IMN logo majestic logo threadwatch logo seochat tools logo