> For the complete documentation index, see [llms.txt](https://kb.logicerp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/writing-formulas-in-the-script-editor/vb.net-formula-examples.md).

# VB.NET Formula Examples

Ready-made **VB.NET** formulas you can copy into the **Script Editor** and change for your own needs. Every example on this page was tested and gives the result shown.

**Before you copy an example:**

* Choose **VB.NET (#@VBNET)** in **Language**.
* Replace the field names with the names in your own **Field List**. **BASIC\_SALARY**, **GROSS\_SALARY**, **DAYS\_WORKED**, **PAID\_HOLIDAYS**, **WORKING\_DAYS**, **EMPLOYEE\_AGE**, **EMPLOYEE\_TYPE**, **DEPARTMENT\_NAME** and **DESIGNATION** are standard payroll fields. **HRA**, **DA**, **PF**, **ESIC**, **PTAX**, **CONVEYANCE** and the other result names stand for allowances and deductions - use the names from your company's allowance list.
* The rates and limits are only examples - use your own.
* Test with **Script Watch** before you save. See **Script Editor**.

### **1. A percentage of a field**

```
HRA = BASIC_SALARY * 40 / 100
```

* With a basic salary of 18,000, **HRA** is 7200.

### **2. An upper or lower limit - Math.Min and Math.Max**

```
PF = Math.Min(BASIC_SALARY, 15000) * 12 / 100
```

* **Math.Min** takes the smaller of the two values, so PF is worked out on at most 15,000. With a basic salary of 20,000, **PF** is 1800; with 12,000 it is 1440.

```
DA = Math.Max(GROSS_SALARY * 10 / 100, 1500)
```

* **Math.Max** takes the larger value, so DA is never less than 1,500. With a gross salary of 10,000, **DA** is 1500.

### **3. Based on the days worked**

```
If WORKING_DAYS > 0 Then
    CONVEYANCE = 1600 * (DAYS_WORKED + PAID_HOLIDAYS) / WORKING_DAYS
Else
    CONVEYANCE = 0
End If
CONVEYANCE = Math.Round(CONVEYANCE, 0, MidpointRounding.AwayFromZero)
```

* With 22 days worked, 2 paid holidays and 26 working days, **CONVEYANCE** is 1477 (1600 x 24 / 26 = 1476.92, rounded).
* The **If WORKING\_DAYS > 0** line makes sure the formula never divides by 0 - see **Common mistakes** below.

### **4. Rounding**

| Write                                               | Result  | What it does                                         |
| --------------------------------------------------- | ------- | ---------------------------------------------------- |
| `Math.Round(1234.565, 2)`                           | 1234.56 | to 2 decimals                                        |
| `Math.Round(2.5)`                                   | 2       | to the nearest **even** number when exactly half way |
| `Math.Round(2.5, 0, MidpointRounding.AwayFromZero)` | 3       | half way always goes up - the usual rounding         |
| `Math.Ceiling(1476.2)`                              | 1477    | always up to a whole number                          |
| `Int(1476.9)`                                       | 1476    | always down to a whole number                        |
| `Math.Ceiling(1476.2 / 10) * 10`                    | 1480    | up to the next 10                                    |
| `Math.Floor(1476.9 / 100) * 100`                    | 1400    | down to the 100 below                                |

* For rupee amounts, use `Math.Round(value, 0, MidpointRounding.AwayFromZero)` - plain `Math.Round(value)` rounds 2.5 down to 2.

### **5. Slabs - Select Case**

```
Select Case GROSS_SALARY
    Case Is <= 10000
        PTAX = 0
    Case Is <= 15000
        PTAX = 150
    Case Else
        PTAX = 200
End Select
```

* The first **Case** that fits is used. With a gross salary of 12,000, **PTAX** is 150; with 15,000.50 it is 200.
* Write the slabs from the lowest up, each with **Case Is <=**, so no amount falls between two slabs.

### **6. Two conditions together - AndAlso**

```
If EMPLOYEE_TYPE = "PERMANENT" AndAlso DAYS_WORKED >= 25 Then
    ATTENDANCE_BONUS = 500
Else
    ATTENDANCE_BONUS = 0
End If
```

* **AndAlso** - both must be true. **OrElse** - one of them is enough.
* A permanent employee with 25 days worked gets 500; a contract employee with 26 days gets 0.

### **7. No deduction above an age**

```
If EMPLOYEE_AGE >= 58 Then
    PF = 0
Else
    PF = Math.Min(BASIC_SALARY, 15000) * 12 / 100
End If
```

* At age 59, **PF** is 0.

### **8. Comparing text safely**

```
If UCase(Trim(DEPARTMENT_NAME)) = "SALES" Then
    INCENTIVE = BASIC_SALARY * 5 / 100
Else
    INCENTIVE = 0
End If

If UCase(DESIGNATION).Contains("MANAGER") Then
    SPECIAL_ALLOWANCE = 2000
Else
    SPECIAL_ALLOWANCE = 0
End If
```

* **UCase** turns the text into capitals and **Trim** removes spaces at the ends, so " sales " still matches "SALES".
* **.Contains** checks for a word anywhere in the text: "Area Manager" matches "MANAGER".
* With a basic salary of 20,000, **INCENTIVE** is 1000 and **SPECIAL\_ALLOWANCE** is 2000.

### **9. Several steps with your own working values**

```
Dim DayRate As Double
Dim AbsentDays As Double

If WORKING_DAYS > 0 Then
    DayRate = GROSS_SALARY / WORKING_DAYS
Else
    DayRate = 0
End If

AbsentDays = WORKING_DAYS - DAYS_WORKED - PAID_HOLIDAYS
If AbsentDays < 0 Then AbsentDays = 0

LOP_DEDUCTION = Math.Round(DayRate * AbsentDays, 2)
```

* **Dim** declares a working value that is not a field. Working values are not saved anywhere; only fields are.
* A short **If** can be written on one line, with no **End If**: `If AbsentDays < 0 Then AbsentDays = 0`.
* With a gross salary of 26,000, 26 working days, 22 days worked and 2 paid holidays, **LOP\_DEDUCTION** is 2000.

### **10. Stop early - Return**

```
If DAYS_WORKED = 0 Then
    ESIC = 0
    Return
End If

ESIC = Math.Ceiling(GROSS_SALARY * 0.75 / 100)
```

* **Return** ends the formula at once; the fields keep the values given so far.
* With no days worked, **ESIC** is 0. With 20 days worked and a gross salary of 18,000, **ESIC** is 135.

### **11. Your own function**

```
SPECIAL_ALLOWANCE = RoundUpTo(GROSS_SALARY * 7 / 100, 50)

Function RoundUpTo(ByVal Amount As Double, ByVal Unit As Double) As Double
    If Unit <= 0 Then Return Amount
    Return Math.Ceiling(Amount / Unit) * Unit
End Function
```

* A **Function** you write once can be used many times in the formula. It can be written before or after the lines that use it.
* With a gross salary of 24,000, **SPECIAL\_ALLOWANCE** is 1700 (7% is 1,680, rounded up to the next 50).

### **Common mistakes**

* **Dividing by 0.** `GROSS_SALARY / WORKING_DAYS` with 0 working days does not stop with a message - it gives a value that is not a usable number. Check first, as in examples 3 and 9.
* **/ and \ are different.** `7 / 2` is 3.5; `7 \ 2` is 3 (the decimals are dropped); `7 Mod 2` is 1 (the remainder).
* **Text needs double quotes** and must match capital letters exactly - use **UCase** as in example 8.
* **Every If needs an End If**, unless the whole If is on one line.
* **Store the result**: `HRA = BASIC_SALARY * 0.4`, not just `BASIC_SALARY * 0.4`.
* If something does not work, press **Check & Fix** - it corrects the usual mistakes and lists the rest by line.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/writing-formulas-in-the-script-editor/vb.net-formula-examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
