How to use DIV and MOD

Alex_Chow
Member Posts: 5,063
Sorry guys, I keep having to search to get the results. For some reason DIV and MOD usage are not explained anywhere on the online help. So I just want to put this here so I can book mark to it. And yes, I do use mibuso for some of my code repository.
x := 5 DIV 2
y := 5 MOD 2
RESULT:
x = 2
y = 1
x := 5 DIV 2
y := 5 MOD 2
RESULT:
x = 2
y = 1
Confessions of a Dynamics NAV Consultant = my blog
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book
0
Comments
-
DIV is integer division. 5 DIV 2 equals 2 because 2 times 2 equals 5 or less.
MOD is Modulus (I think, not quite sure about the word) but gives you the remainder of integer division.
So ((5 DIV 2) * 2) + (5 MOD 2) = 5.
I always have to put it on a test form to figure out which one is which :oops:0 -
:-k i doesn't understand the problem (if there is one)Do you make it right, it works too!0
-
garak wrote::-k i doesn't understand the problem (if there is one)
No, there is no problem, just lack of documentation. But these functions is common in other languages and it is not problem to find the meaning...
http://en.wikipedia.org/wiki/Modulo_operation
I think that DIV do not need more explanation... ;-)0 -
ah, ok ... (never checked if modulo or division is in the online help..)
is this known :?:for i := 10 DownTo 1 do begin end;
Do you make it right, it works too!0 -
My NAV reference guide, is actually a Pascal developers guide.
Great book, an it has all this stuff in it.David Singleton0 -
Well.. I just put it on here because I get confused which is which as well. So now we all know in a thread with a clear topic.
And knowing is half the battle.Confessions of a Dynamics NAV Consultant = my blog
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book0 -
You should post these in the book wiki, that's a really good place for reference material like this.0
-
HI ALEX
The Integer Divide (DIV) OperatorThe integer divide operator is used only as a binary operator. Its purpose is to
divide the numeric term preceding it by the numeric term following it. The result
type of this division is always of type Integer. If the second term is zero (0), a
run-time error occurs. Any decimals that resulted from an ordinary division are
dropped. Therefore, the result of 17 DIV 8 is 2, whereas the result of 17 DIV 9 is
1.
The Modulus (MOD) OperatorThe modulus operator (or the remainder operator) is used only as a binary
operator. Its purpose is to divide the numeric term preceding it by the numeric
term following it by using the integer division method and then return the
remainder of that division. The result of this operation is always of type Integer.
If the second term is zero (0), a run-time error occurs. The following shows
examples of modulus operator usage.
• 17 MOD 8 = 1
• 17 MOD 9 = 8
The modulus operator requires two numbers. The first number is the one that is
converted by using the modulus function and the second number represents the
number system being used. By definition the number system starts at zero and
ends at the second number minus one. For example, if the second number is ten,
the number system that is used is from zero to nine. Therefore, the modulus
represents what the first number converts to, if the numbering system only had
the number of values indicated by the second number and the first number is
forced to restart from zero.
The following example shows several modulus operations:
• 15 modulus 10 is 5 (because 9 is the last number available, 10 is
represented by going back from the start, or zero, 11 is 1, 12 is 2, and
so on)
• 6 modulus 10 is 6
• 10 modulus 10 is 0
• 127 modulus 10 is 7
The result is the same if the first number is divided by the second by using an
integer only and the remainder is returned as the value.smile always
shona
That which you seek inside is that which you find outside0 -
I use it in a check when we split PO's due to 1/2 30 days 1/2/ 60 days terms.
We used to manually cut the Qty to Invoice in half , which therefor cuts the invoice amount in half.
Then we post with 30 days terms. Then we post the (remaining) other half with 60 days terms.
Giving us two vendor ledger entries with the correct terms. But then we started getting these terms on PO's that had 100 items and manually this was exhausting. So using MOD I can see if the amount is cleanly divisible by 2 & if not it gives us a message to check the Item and handle that manually.
Hail MOD!!
Remainder := Purchline."Quantity Received" MOD 2;
IF Remainder <> 0 THEN MESSAGE('Item %1 Needs Rounding',Purchline."No.");OnPush() IF NOT DIALOG.CONFIRM(Text1,TRUE) THEN EXIT ELSE Purchline.RESET; Purchline.SETRANGE(Purchline."Document Type","Document Type"); Purchline.SETRANGE(Purchline."Document No.","No."); IF Purchline.FIND('-') THEN REPEAT IF Purchline."Quantity Received" <>0 THEN BEGIN Purchline."Qty. to Invoice" := (Purchline."Quantity Received" / 2); Remainder := Purchline."Quantity Received" MOD 2; IF Remainder <> 0 THEN MESSAGE('Item %1 Needs Rounding',Purchline."No."); Purchline.VALIDATE("Qty. to Invoice"); Purchline.MODIFY; END; UNTIL Purchline.NEXT = 0;
0 -
Hello,
Has anyone tried to use DIV in combination with BigInteger in NAV2009 RTC?
If you execute following code in NAV2009 you'll get following messages:LText := FORMAT(DELCHR(PBankAccountNo,'=',DELCHR(PBankAccountNo,'=','0123456789')) + 'BE' + '00'); FOR i := 1 TO STRLEN(LText) DO BEGIN LConvertText := LConvertText + FORMAT(GetRomanNoIBAN(COPYSTR(LText, i, 1))); END; EVALUATE(LAccountNo, LConvertText); LRest := LAccountNo - ((LAccountNo DIV 97) * 97); MESSAGE(format(LREST));
Message in Classic Client (which is correct):
Microsoft Dynamics NAV Classic
88
OK
Message in Role Tailored Client (which is incorrect):
Microsoft Dynamics NAV
235002258041864280
OK
Anyone got any ideas?0 -
can you explain div with negative numbers. For example: -2 mod 4, or -3 mod 4.0
-
The result of DIV tells you, how many times the divisor fits within the dividend and is the same as the full decimal expansion of the result with all decimals cut off.
The reminder is dividend minus quotient times dividend.
so, if
A DIV B = Q Then
A MOD B = A - B * Q
-2 DIV 4 = 0
-2 MOD 4 = -2 - 0 * 4 = -2
-3 DIV 4 = 0
-3 MOD 4 = -3 - 0 * 4 = -3
-5 DIV 4 = -1
-5 MOD 4 = -5 - (-1) * 4 = -5 + 4 = -10
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions