How to Line Up Decimal Numbers in Just Basic 1.01

For those who need a simple Basic language without the complications of objects and frameworks, Just Basic from Liberty Basic is the perfect choice. The language can be downloaded at no cost and anyone familiar with traditional Basic can be up to speed almost immediately. Although the language lacks statements like Print Using and Format, it does offer the Tab and Space$ functions and these are all that are needed to line up decimal numbers. By following a few simple steps, you can create a formatted grocery list using Just Basic.

Things You'll Need

  • Just Basic version 1.01
Show More

Instructions

    • 1

      Open Just Basic and create a new project. Click “File,” then “New Basic Source File.”

    • 2

      Use the Print statement to print the title and column headers as follows:

      Print "Grocery List"
      print
      print "Qty"; tab(5); "Description"; tab(30); " Price"

      The Print statement sends any text that follows it to the print window. The Tab function moves the cursor to that column of the print line. This allows you to place the column headers directly over the data.

    • 3

      Format the first line of grocery items as follows:

      print 5; tab(5); "Apples"; tab(30); FormatNum$ (1.25, 4)

      This statement places the quantity 5 at the first column, tabs over to the fifth column and places the word “Apples” then tabs to column 30 to place the amount 1.25. The FormatNum$ function adds leading spaces to each number to align by decimal point.

    • 4

      Add a few more lines of grocery items to the list as follows:

      print 1; tab(5); "Milk"; tab(30); FormatNum$(2.29, 4)
      print 1; tab(5); "Pot Roast"; tab(30); FormatNum$(12.95, 4)
      print 1; tab(5); "Chocolate Bar"; tab(30); FormatNum$(0.59, 4)

    • 5

      Print the total.

      print tab(5); " Total"; tab(30); FormatNum$(1.25 + 2.29 +12.95 + 0.59, 4)

      This line places the word Total below the item descriptions then places the total below the numbers.

    • 6

      Define the FormatNum$ function. Place this code below the Print statements.

      function FormatNum$ (x, i)
      s$ = str$(x)
      l = len(str$(int(x)))
      if l < i then
      s$ = space$(i - l) + s$
      end if
      FormatNum$ = s$
      end function

      The first line declares the function with x containing the amount and I containing the number of characters desired before the decimal point.

      Line 2 saves the number as a character string in the variable s$.

      Line 3 counts the number of digits currently in front of the decimal point. If the number is 12.34, the integer is 12 so l will contain the value 2.

      Line 3 through 5 uses the Space$ function to add spaces to the front of s$. If l is 2 and the desired spaces (i) is 4, we need to add two additional spaces in front of s$.

      Line 6 returns the formatted number back to the calling program.

    • 7

      Run the program. Click the SHIFT and F5 keys together to see the results.

Tips & Warnings

  • Once the program runs, click “File” then “Print” to print the report.

Related Searches:

References

Comments

Related Ads

Featured