TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Math.Round Example

Examine the Math.Round Function, and use the MidpointRounding argument.
Math.Round. Numbers can be rounded in many ways. In the .NET Framework, Math.Round provides built-in logic. With it we round with special options—away from zero, or to even. It acts on many numeric types, including Double and Decimal.Double
Example. This program calls Math.Round on the Double 123.45. With no options, it rounds this number to 123. With a second option of 1, we round to one decimal place. This yields the values 123.5 (for AwayFromZero) and 123.4 (for ToEven).

AwayFromZero: With a positive number, this option will round up—so 123.45 becomes 123.5.

ToEven: This will round to an even number—so 123.45 becomes 123.4 because 4 is an even number and 5 is not.

VB.NET program that calls Math.Round Module Module1 Sub Main() ' Call Math.Round on this Double. Dim before As Double = 123.45 Dim after1 As Double = Math.Round(before, 1, MidpointRounding.AwayFromZero) Dim after2 As Double = Math.Round(before, 1, MidpointRounding.ToEven) Dim after3 As Double = Math.Round(before) Console.WriteLine(before) Console.WriteLine(after1) Console.WriteLine(after2) Console.WriteLine(after3) Console.WriteLine() ' Use on this Decimal. Dim before2 As Decimal = 125.101 Dim after4 As Decimal = Math.Round(before2) Dim after5 As Decimal = Math.Round(before2, 1) Console.WriteLine(before2) Console.WriteLine(after4) Console.WriteLine(after5) End Sub End Module Output 123.45 123.5 123.4 123 125.101 125 125.1
Notes, program. We also use the Math.Round overloads on the Decimal type. These have the same results and logic as the Double type. The Math Class is designed to support many numeric types, including Double and Decimal and others.

Also: It is possible to provide two arguments to Math.Round. The second Integer specifies the number of decimal places to round to.

Integer
Summary. Rounding functionality in the .NET Framework is built-in and easy-to-use. It is not needed to develop custom Functions in most programs. We instead invoke the Math.Round Shared Function, with one to three arguments.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf