How can I always round up decimal values to the nearest integer value?
Asked Answered
T

4

5

On a report I have the following code for a field:

=Sum([PartQty]*[ModuleQty])

Example results are 2.1 and 2.6. What I need is for these value to round up to the value of 3. How can I change my field code to always round up the results of my current expression?

Telpherage answered 1/4, 2010 at 10:17 Comment(2)
Sorry Sam. Not been using this site long, how do you vot and accept answers, you are the first peron to mention this. You have been very helpful. ShaunTelpherage
no problem :). to vote click on the arrows above or below the number in the top left of the answers. click the up arrow to vote that the answer is useful, the down arrow if it is not. leave a comment if you downvote to say why. Click the tick symbol next to the answer that you accept as being the answer to your question. it'll go green to show that it is the accepted solution.Apocalyptic
A
1

you could do

=Int(Sum([PartQty]*[ModuleQty]))+1

I think. That would get the Int part of the sum (2) and then add 1. you might need to be a little more clever as this will probably give you 3 even if the sum is exactly 2, which is probably not what you want.

not tested it but something along these lines might work (access syntax is not that great, but should give you the right idea) :

Iif(Sum([PartQty]*[ModuleQty])-Int(Sum([PartQty]*[ModuleQty]))=0,
     Sum([PartQty]*[ModuleQty]),
     Int(Sum([PartQty]*[ModuleQty]))+1)
Apocalyptic answered 1/4, 2010 at 10:21 Comment(4)
Hi Sam. You are right about your first idea, if the value is 2 it will round up to 3. Will try the next idea soon. CheersTelpherage
Hi Sam. I have tried the new code, I keep getting an error message regarding the comma's. Shaun.Telpherage
Hi Sam I have replaced the first comma with Then and the second comma with = still not working getting errorsTelpherage
techonthenet.com/access/functions/advanced/iif.php might help with the syntax... this: databasedev.co.uk/count_values_countiif.html implies that you can use it in a report. try breaking it down to see if you can get the syntax correct: IIf(2-1=0,2,1+1) should be correct syntax from what I can see. and make sure you are using IIF and not IFApocalyptic
P
8

This is an old Access trick I learned a very long time ago, and it makes use of the way Access handles fractional, negative numbers. Try this:

-Int(-[DecimalValue])

It's odd, but it will always round your numbers up to the nearest whole number.

Prothalamion answered 20/12, 2012 at 16:15 Comment(8)
Yes, I agree. I'm not sure if this is a bug but it's worked for years and, in fact, I used it just the other day in Access 2010. So, it's still useful. Also, is quite terse and as long as you understand the purpose of the syntax, it is the easiest way to express that you want to "round up" a number.Prothalamion
I am fairly sure it is not a bug, it is rounding down, isn't it, in each case? -1.4=-2, 1.4=1Stouffer
Yes, but the odd thing is why does it always round down negative numbers? i.e. -1.4=-2 and -1.9 = -2. Should rounding any other, positive integer behave the same way when rounding them?Prothalamion
1 is 1.1 to 1.9 rounded down, -1.1 to -1.9 rounded down is -2, yesno? -1 is higher than -1.1, so -1 would be rounding up.Stouffer
@Remou: Yes, you are correct. The reason why this is peculiar is negative numbers behave differently than positive numbers. Any number between -x.1 and -x.9 always rounds to -x. However, positive numbers follow "Banker's rounding" rules. (see: en.wikipedia.org/wiki/Banker%27s_rounding#Round_half_to_even)Prothalamion
Sorry, but my point is that negative numbers behave just the same as positive numbers, they round down with Int.Stouffer
With Round(-1.1,0)=-1, Round(-1.9,0)=-2 Round is not the same as Int.Stouffer
Ah, you are correct. I was forgetting, what I am reference is when you pass the value through the ROUND() function. ROUND uses Bankers rules, Int() always rounds down. Apologies.Prothalamion
E
2

Test this: Round(yournumber + 0.5, 0)

Escallop answered 24/7, 2014 at 12:23 Comment(0)
A
1

you could do

=Int(Sum([PartQty]*[ModuleQty]))+1

I think. That would get the Int part of the sum (2) and then add 1. you might need to be a little more clever as this will probably give you 3 even if the sum is exactly 2, which is probably not what you want.

not tested it but something along these lines might work (access syntax is not that great, but should give you the right idea) :

Iif(Sum([PartQty]*[ModuleQty])-Int(Sum([PartQty]*[ModuleQty]))=0,
     Sum([PartQty]*[ModuleQty]),
     Int(Sum([PartQty]*[ModuleQty]))+1)
Apocalyptic answered 1/4, 2010 at 10:21 Comment(4)
Hi Sam. You are right about your first idea, if the value is 2 it will round up to 3. Will try the next idea soon. CheersTelpherage
Hi Sam. I have tried the new code, I keep getting an error message regarding the comma's. Shaun.Telpherage
Hi Sam I have replaced the first comma with Then and the second comma with = still not working getting errorsTelpherage
techonthenet.com/access/functions/advanced/iif.php might help with the syntax... this: databasedev.co.uk/count_values_countiif.html implies that you can use it in a report. try breaking it down to see if you can get the syntax correct: IIf(2-1=0,2,1+1) should be correct syntax from what I can see. and make sure you are using IIF and not IFApocalyptic
A
0

Volunteer for a Fire Dept. They get paid for any call over an hour 1.0. I took the date difference with a "." format change from ":". (1:30 = 1.3)

Duration: (DateDiff("n",[StartTimeCalc],[EndTimeCalc]))\60 & Format((DateDiff("n",[StartTimeCalc],[EndTimeCalc])) Mod 60,"\.00")

So now the duration under an hour shows as "0". A duration of 12.1 which would be "13" shows as "12".

Duration1: Int(([Duration]-0.6)+0.59)

Table Snippet

Atropine answered 10/6, 2024 at 18:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.