Round Up a Decimal

J

jlo

I have a field called Weight. I want to round the decimals to the next whole
number. For instance:

1.1 thru 1.9 should round to 2
2.1 thru 2.9 should round to 3.

Any ideas?
 
J

Jerry Whittle

In a query with the proper field name:

RoundedUp: IIf([TheField] = Int([TheField]), [TheField], Int([TheField]) +1)

If you have some negative numbers, you may want to read Help on the
differences between the Int and Fix functions.
 
J

John Spencer

This expression will round up your number
-Int(-[TheField])
It works very well with positive numbers, with negative numbers the results
are rounded up, however that means to the integer to the left on the number
line. -25.1 is rounded up to -25 not -26 BECAUSE -25 is larger than -25.1

If you need to round negative numbers to a higher absolute value then you can
use the more complex expression below (-25.1 will go to -26 and +25.1 will go
to +26).

-Int(-abs(x)) * sgn(Nz(x,0))


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
K

Keven Denen

I have a field called Weight.  I want to round the decimals to the nextwhole
number.  For instance:

1.1 thru 1.9 should round to 2
2.1 thru 2.9 should round to 3.

Any ideas?

Public Function Ceiling(dNumber As Double, Optional dFactor As Double
= 1) As Double
Ceiling = (Int(dNumber / dFactor) - (dNumber / dFactor - Int
(dNumber / dFactor) > 0)) * dFactor
End Function

Public Function Floor(dNumber As Double, Optional dFactor As Double =
1) As Double
Floor = Int(dNumber / dFactor) * dFactor
End Function

I use these two functions to handle rounding in most of my Access
databases. Handles negative and positive numbers.

Ceiling(-2.4) = 2
Ceiling(3.5) = 4
Ceiling(128.3,10) = 130 (round to the next highest multiple of 10)
Floor(-2.4) = 3
Floor(3.5) = 3
Floor(128.3,10) = 120 (round to the next lowest multiple of 10)

Keven Denen
 
J

Jerry Whittle

I like it. That's a keeper!
--
Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


John Spencer said:
This expression will round up your number
-Int(-[TheField])
It works very well with positive numbers, with negative numbers the results
are rounded up, however that means to the integer to the left on the number
line. -25.1 is rounded up to -25 not -26 BECAUSE -25 is larger than -25.1

If you need to round negative numbers to a higher absolute value then you can
use the more complex expression below (-25.1 will go to -26 and +25.1 will go
to +26).

-Int(-abs(x)) * sgn(Nz(x,0))


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
I have a field called Weight. I want to round the decimals to the next whole
number. For instance:

1.1 thru 1.9 should round to 2
2.1 thru 2.9 should round to 3.

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top