VB.NET || How To Round A Number To The Nearest X Using VB.NET
The following is a module with functions which demonstrates how to round a number to the nearest X using VB.NET.
This function has the ability to either round a number to the nearest amount, always round up, or always round down. For example, when dealing with money, this is good for rounding a dollar amount to the nearest 5 cents.
1. Round – Nearest
The example below demonstrates the use of ‘Utils.RoundAmount‘ to round a number to the nearest 5 cents.
The optional function parameter determines they type of rounding to perform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
' Round - Nearest ' Declare values to round Dim values = New Decimal() {19.28D, 31.22D, 19.91D, 19.87D, 0.05D} ' Declare step amount Dim stepAmount = 0.05D ' Round to nearest amount For Each value In values Debug.Print($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount)}") Next ' expected output: ' Value: 19.28, Rounded: 19.3 ' Value: 31.22, Rounded: 31.2 ' Value: 19.91, Rounded: 19.9 ' Value: 19.87, Rounded: 19.85 ' Value: 0.05, Rounded: 0.05 |
2. Round – Up
The example below demonstrates the use of ‘Utils.RoundAmount‘ to always round a number up to the nearest 5 cents.
The optional function parameter determines they type of rounding to perform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
' Round - Up ' Declare values to round Dim values = New Decimal() {19.28D, 31.22D, 19.91D, 19.87D, 0.05D} ' Declare step amount Dim stepAmount = 0.05D ' Round up to nearest amount For Each value In values Debug.Print($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount, Utils.RoundType.Up)}") Next ' expected output: ' Value: 19.28, Rounded: 19.3 ' Value: 31.22, Rounded: 31.25 ' Value: 19.91, Rounded: 19.95 ' Value: 19.87, Rounded: 19.9 ' Value: 0.05, Rounded: 0.05 |
3. Round – Down
The example below demonstrates the use of ‘Utils.RoundAmount‘ to always round a number down to the nearest 5 cents.
The optional function parameter determines they type of rounding to perform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
' Round - Down ' Declare values to round Dim values = New Decimal() {19.28D, 31.22D, 19.91D, 19.87D, 0.05D} ' Declare step amount Dim stepAmount = 0.05D ' Round down to nearest amount For Each value In values Debug.Print($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount, Utils.RoundType.Down)}") Next ' expected output: ' Value: 19.28, Rounded: 19.25 ' Value: 31.22, Rounded: 31.2 ' Value: 19.91, Rounded: 19.9 ' Value: 19.87, Rounded: 19.85 ' Value: 0.05, Rounded: 0.05 |
4. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Dec 1, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils Public Enum RoundType Nearest Up Down End Enum ''' <summary> ''' Rounds a number to the nearest X ''' </summary> ''' <param name="value">The value to round</param> ''' <param name="stepAmount">The amount to round the value by</param> ''' <param name="type">The type of rounding to perform</param> ''' <returns>The value rounded by the step amount and type</returns> Public Function RoundAmount(value As Decimal, stepAmount As Decimal _ , Optional type As RoundType = RoundType.Nearest) As Decimal Dim inverse = 1 / stepAmount Dim dividend = value * inverse Select Case type Case RoundType.Nearest dividend = Math.Round(dividend) Case RoundType.Up dividend = Math.Ceiling(dividend) Case RoundType.Down dividend = Math.Floor(dividend) Case Else Throw New ArgumentException($"Unknown type: {type}", NameOf(type)) End Select Dim result = dividend / inverse Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
5. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Dec 1, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Declare values to round Dim values = New Decimal() {19.28D, 31.22D, 19.91D, 19.87D, 0.05D} ' Declare step amount Dim stepAmount = 0.05D ' Round to nearest amount For Each value In values Display($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount)}") Next Display("") ' Round up to nearest amount For Each value In values Display($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount, Utils.RoundType.Up)}") Next Display("") ' Round down to nearest amount For Each value In values Display($"Value: {value}, Rounded: {Utils.RoundAmount(value, stepAmount, Utils.RoundType.Down)}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply