Use the VB. NET format function to implement rounding.

Source: Internet
Author: User

After a long period of development, VB. NET has become very familiar to many users. Here I will share my personal understanding and discuss it with you. In VB. net should use VB. for the netformat function, both CINT and clng can only obtain the integer part, whereas VB. net format function can get the number of decimal places you specify.

 

 
Public Function round (numasvariant, naslong) as string round = format (Num, IIF (n> 0, "0. "& string (n," 0 ")," 0 ") end Function

 

 

Description: num: the value to be rounded. Because the value to be rounded down may be double, single, or other data types, the num is declared as variant here. If the num is declared as double, and the value to be rounded down is a single type, an incorrect data type occurs. However, declaring num as variant can avoid this error, or using a value transfer call (byval) to avoid this error. In fact, I am not sure whether this idea is correct. If it is incorrect, please confirm it. Thank you. N: N digits below the decimal point. The round function returns a string.

Example:

 

 
Private sub commandateclick () dim A as double A = 1234.56789 debug. print fix (a) debug. print int (a) debug. print CINT (a) debug. print clng (a) debug. print round (A, 0) debug. print round (A, 3) end sub

After execution, the following results are displayed in the VB real-time operation window:

 

 

1234

1234

1235

1235

1235

1234.568

Fix and INT belong to unconditional rounding, but there are still some slight differences between them in dealing with negative numbers. Please refer to vbhelp on your own; CINT and clng also have the rounding function, however, only the integer part can be obtained. format can be rounded to the specified decimal place. It is worth mentioning that the second parameter of VB. netformat uses the difference between "0" and:

 

 
Format (123.4, ". 000") 'output 123.400 format (123.4, ". ###") 'output 123.4

 

 

That is to say, if the second parameter uses "0", the excess 0 will be automatically added. Sometimes it is expected to be done for better arrangement; if "#" is used, 0 is not automatically added.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.