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.