How to Return Next Upper Integer for any Real Number in Basic (80405)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1
  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • 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:

  1. 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.
  2. Add a text box to Form1 (Text1).
  3. Add another text box to Form1 (Text2).
  4. Add the following code to the Text1_Change event procedure for Text1:
         Sub Text1_Change ()
            text2.text = Str$(-Int(-Val(text1.text)))
         End Sub
    						
  5. 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.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB80405