Select to view content in your preferred language

Rounding decimals up using the field calculator

6047
11
Jump to solution
02-23-2011 12:48 AM
EdelSødal_Pedersen
Deactivated User
Hi,

I'm new to python and couldn't find an answer elsewhere to this problem. And I guess it's not a big deal actually, but I just don't get it...

Code Block:
def tvol(a, b, area):
if (a >=2):
  return a*b/100

Expression:
tvol(!a!, !b!, !AREA!)

How do I get rounded values in the end, like VB would do. F. ex. 35.5 --> 36 and not 35, 35.4 --> 35.
Someone having an idea how to put this into the above script?

Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus
to avoid integer division, one of the inputs must be cast as a float and the simplest way to do that in your case is to divide by 10.0 not 10

View solution in original post

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus
did you play around with python's round and/or int methods?
0 Kudos
EdelSødal_Pedersen
Deactivated User
I tried for example:
Code Block:
def tvol(a, b, area):
if (a >=2):
return round (a*b/100), 2)

The script works, but still the result is the same, because I think it's the standard of python to round that way.

I found some other infos arround that using ROUND_HALF_UP etc. or as described here:
http://docs.python.org/library/decimal.html

But I can't transfere the idea to the field calculator... :confused:
0 Kudos
GerryGabrisch
Frequent Contributor
try this...

def tvol(a, b, area):
    if (a >=2):
    return int(round(a*b/100))
0 Kudos
EdelSødal_Pedersen
Deactivated User
Thanks for the hint, but unfortunately I get the same result ... :confused:
0 Kudos
MichaelStead
Frequent Contributor
Never tried it, but it looks like some variation on ceiling may be available

http://docs.python.org/library/math.html
0 Kudos
EdelSødal_Pedersen
Deactivated User
Thanks for the link. math.ceil() really should do the trick!
It's also explained in section 5.4 here: http://docs.python.org/library/stdtypes.html

But this is really weird.
I've defined 3 new fields in the table just to make sure it's the right field type. So I have three fields AB and C, all are double.
The script for field c:
math.ceil ( !A! * !B! /10)

A=43, B=2 and I get C=8 (8.6, should result in 9)
Even if I just calculate !A! * !B! /10 within the field calculator, I do not even get decimals here (8.6, just returning 😎
So I'm wondering if there are some generell adjustments somewhere ... which are not correct 😞
0 Kudos
DanPatterson_Retired
MVP Emeritus
to avoid integer division, one of the inputs must be cast as a float and the simplest way to do that in your case is to divide by 10.0 not 10
0 Kudos
EdelSødal_Pedersen
Deactivated User
Thanks! 😄

Never would have figured out that it would be 10.0 instead of 10!

round(!A!* !B! /10.0) is giving the result I was looking for!

Thanks and a nice day to all of you!
0 Kudos
ChrisGraves
Regular Contributor
Hi

I am experiencing the same issue where I need to round even numbers (ie 56.5) up and have tried the following script in the field calculator (using Python

parser) but it returns an error:

round(!A!* !B! /10.0)

I'm not really up with Python so not sure what needs to be changed in the script.

Thanks in advance,

Chris
0 Kudos