Wednesday, December 17, 2008

[vb6] Rounding numbers

If you are displaying real numbers, you might like to display them in rounded format, and possibly with a fixed number of digits after the decimal separator. The format function rounds numbers if you specify a an expression for its format parameter where zeros indicate that the digit is valid and "\0" indicate that the function should append a trailing zero. For example:
    Dim n As Single
n = 23.647
Debug.Print Format$(n, "0.000")
Debug.Print Format$(n, "0.00\0")
Debug.Print Format$(n, "0.0\0\0")
Debug.Print Format$(n, "0.\0\0\0")
Debug.Print Format$(n, "0.00")
If your decimal separator is the dot character, the output from this code is:
    23.647
23.650
23.600
24.000
23.65

No comments:

Post a Comment