This document is the seventh in a set of tutorials on writing calculations in Service Desk. Each topic can be read independently but for an introduction to calculations please see the following document: Calculation Writing Tutorial - 1. What is a calculation? What is Boo?.
Some experience in object and process design is recommended, as well as the basic concept of calculations as covered in the first tutorial (linked above) and also tutorial number 5: Calculation Writing Tutorial - 5. Boo language features versus .NET classes.
Number Data Types
When working with numbers in calculations we are actually working with multiple types of data. In Service Desk you can create attributes with Int16, Int32, and Decimal data types to hold numbers. Int16 and Int32 are very similar - they can hold Integer values (ie. whole numbers) up to a certain value. The only real difference is the maximum value they can hold and how much space in the database it takes to hold it. Decimal values can include a decimal place and fractions of a whole number. A popular use for Decimals in Service Desk is to allow two decimal places and hold monetary values in Pounds and Pence (or Dollars or Euros and Cents).
Each data type has its own set of rules that can differ slightly to other types so when working with them you sometimes need to take extra consideration. We go into that later in this topic.
While working with Boo you may end up with variables that use the Single or Double data type. For the purpose of calculation writing think of these as the same as a Decimal.
When working with numbers in calculations the main use will be simple arithmetic - addition, subtraction, multiplication, and division. The first three are very straight forward to achieve in Boo if you stick to whole numbers:
Value = 2 + 3 SmallerValue = Value - 2 LargerValue = Value + SmallerValue EvenBiggerValue = Value * LargerValue
In this example the variable Value is set to 5 (2 plus 3). The variable SmallerValue is set to 3 (Value which is 5 minus 2). The variable LargerValue is set to 8 (Value which is 5 and SmallerValue which is 3). The variable EvenBiggerValue is set to 40 (Value which is 5 multiplied by LargerValue which is 8). As you can see from this you can use hard coded numbers, variables, or a combination of the two in your equation. You can also get more complicated than using just two values at a time:
Value = 5 + 4 - (3 * 2) + 1
In this example the variable Value is set to 4. The equation chains multiple numbers and uses parentheses to note the order in which they are calculated. The section (3 * 2) is calculated first (3 multiplied by 2 = 6) because it is encased in parentheses which makes the equation actually 5 + 4 - 6 + 1.
Division and working with Decimals
Programming languages like Boo have a great approach of keeping data in their specific data type. However this can create some confusion when working with numbers because sometimes you will want to change the data type. For some you might want to take two Integers and use them to create a Decimal:
ValueA = 5 ValueB = 2 Value = ValueA / ValueB
This example divides 5 by 2, so at first glance you would expect the result to be 2.5. However in this example, the variable Value will be set to 2. Because you inputted two Integers Boo assumed you wants to output an Integer. This is basically the same way you could concatenate two Strings and the result would also be a String. To get around this you must think carefully about the values your calculation could use and what the result might be. Fortunately you can explicitly tell a variable to use a data type of your choice when setting its value:
ValueA as Decimal = 5 ValueB as Decimal = 2 Value = ValueA / ValueB
This example sets Value to the more sensible 2.5. Imagine you have two Int16 attributes on your Incident object called _IntA and _IntB:
Value = Incident._IntA / Incident._IntB
If _IntA is 5 and _IntB is 2 this example will set the variable Value to 2. To get the more sensible result you must again explicitly set the data types:
ValueA as Decimal = Incident._IntA ValueB as Decimal = Incident._IntB Value = ValueA / ValueB
The values are now automatically converted from Int16 to Decimal values so the variable Value is set to 2.5.
Advanced Arithmetic using .NET Methods
The .NET Framework includes a class called System.Math which includes a host of Static methods to perform more complex equations:
Value = 2.7 ValueRounded = Math.Round(Value) ValueFloor = Math.Floor(Value) ValueSquared = Math.Sqrt(Value) ValueSin = Math.Sin(Value)
In these examples ValueRounded is set to 3, ValueFloor is set to 2, ValueSquared is set to 1.6431676725155 and ValueSin is set to 0.42737988023383. For more information on all the System.Math methods see http://msdn.microsoft.com/en-us/library/system.math_methods.aspx.
As well as the Math class, each data type will have its own set of methods which might overlap with the ones available in System.Math. For example the Decimal class has Round(), Ceiling() and Floor() methods just like the Math class, and also has more specific methods such as Remainder(). As with all .NET classes you can browse their methods and properties on the MSDN website, or a simple online search for ".NET Decimal" will normally get you straight there.