Microsoft KB Archive/80405

= How to Return Next Upper Integer for any Real Number in Basic =

Article ID: 80405

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q80405



SUMMARY
The following formula converts any real number having a non-zero fractional value to the next upper integer x = - Int ( - x) (where x can be any single or double precision number).

The Int function converts a numeric expression to the largest integer less than or equal to the expression.

This information applies to Microsoft QuickBasic versions 1.0, 1.01, 1.02, 2.0, 2.01, 3.0, 4.0, 4.0a, 4.0b, and 4.5 for MS-DOS; Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS; Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS; and Microsoft Visual Basic programming system version 1.0 for Windows.



MORE INFORMATION
In the formula above, using the negative sign (-) before the argument to the Int function causes the Int function to return the next integer lower or equal to the argument. For example, passing the value of 4.7 as the argument in the above formula will cause -4.7 to be passed to the Int function. The result of the Int function will be -5. Applying the negative sign (-) to this result causes the final result to be 5. In the case of negative numbers, values such as -1.7 become -1 when passed as an argument to the above formula. Compare this to the result of -2 when -1.7 is passed to the Int function.

The following is a code example of using the Int function to cause the value 2.7 to be rounded down to 2: x = 2.7 x = Int (x) Print x  'output is 2 The following is a code example of using the formula above to cause the value 2.7 to be rounded up to 3: x = 2.7 x = - Int ( -x ) Print x  'output is 3

Example for Visual Basic
Below are the steps necessary to create a code example in Microsoft Visual Basic 1.0 to convert the contents of one text box and place the result in a second text box:

 Run Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 will be created by default. Add a text box to Form1 (Text1). Add another text box to Form1 (Text2).  Add the following code to the Text1_Change event procedure for Text1: Sub Text1_Change text2.text = Str$(-Int(-Val(text1.text))) End Sub  From the Run menu, choose Start (ALT, R, S).

Enter numeric text in Text1 representing a real number with a decimal point. As numeric text is entered in Text1, the integer representation of the number is displayed in Text2. The value in Text2 will always represent the next equal or higher integer value of the real number in Text1.

Additional query words: QuickBas BasicCom 1.00 2.00 2.10 3.00 4.00 4.50 6.00 7.00 7.10 round rounding

Keywords: KB80405

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.