Results 1 to 4 of 4

Thread: Spacing help!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2014
    Posts
    25

    Spacing help!

    I need help lining up the decimal point for each number. Here is my code:

    ' Program: P06.vb
    ' Author: Anna DeAngelis

    Imports System.IO
    Imports RahimLibrary

    Module P05
    Const AUTHOR As String = "Anna DeAngelis Assignment 6"
    Const HEAD1 As String = "GO-BROKE-AND-LAYOFF MANUFACTURING COMPANY"
    Const HEAD2 As String = _
    " EMPLOYEE_NAME HOURS PAY_RATE GROSS_PAY PENSION NET_PAY "
    Const LINE = "---------------------------------------------------------------------"
    Const FMT = "0.00"

    Sub Main()
    Dim Tokens As StringTokenizer
    Dim Delimiter As Char() = {" ", ",", ";", ":"}
    Dim Diskfile As String = "P06.DAT"

    Console.Clear()
    Dim Name As String
    Dim hours, payRate, grossPay, pension, netPay, totalHours, totalGrossPay, totalPension, totalNetPay As Decimal




    totalHours = 0 : totalGrossPay = 0 : totalPension = 0 : totalNetPay = 0

    Console.WriteLine(AUTHOR & vbNewLine)
    Console.WriteLine(Space(27) & HEAD1 & vbNewLine & Space(4) & _
    HEAD2 & vbNewLine & Space(5) & LINE)

    If Not File.Exists(Diskfile) Then
    Console.WriteLine("File: " & Diskfile & " does not exist")
    Console.WriteLine(vbNewLine & vbNewLine) : Exit Sub : End If

    FileOpen(1, Diskfile, OpenMode.Input)
    While Not EOF(1)

    Tokens = New StringTokenizer(LineInput(1), Delimiter)

    Name = Tokens.NextToken() & " " & Tokens.NextToken()
    Name = Name.PadRight(15)
    hours = Tokens.NextToken()
    payRate = Tokens.NextToken()
    grossPay = CalculateGrossPay(hours, payRate)
    pension = CalculatePension(hours, payRate)
    netPay = grossPay - pension

    totalHours = totalHours + hours
    totalGrossPay = totalGrossPay + grossPay
    totalPension = totalPension + pension
    totalNetPay = totalNetPay + netPay

    Console.Write(Space(6) & Name)
    Console.Write(Space(4) & "{0:F2}", hours)
    Console.Write(Space(7) & "{0:F2}", payRate)
    Console.Write(Space(6) & "{0:F2}", grossPay)
    Console.Write(Space(5) & "{0:F2}", pension)
    Console.WriteLine(Space(4) & "{0:F2}", netPay)

    End While
    FileClose(1)

    Console.Write(Space(5) & LINE & vbCrLf & Space(6) & "TOTALS:")
    Console.Write(Space(12) & "{0:F2}", totalhours)
    Console.Write(Space(16) & "{0:F2}", totalGrossPay)
    Console.Write(Space(4) & "{0:F2}", totalPension)
    Console.WriteLine(Space(3) & "{0:F2}", totalNetPay)
    Console.Write(Space (5) & LINE & vbCrLf & vbCrLf)

    End Sub 'Main()
    '---------------------------Function: CalculateGrossPay---------------------------
    Function CalculateGrossPay (ByVal hours As Decimal,
    ByVal rate As Decimal) As Decimal

    Dim gross as Decimal

    If (hours > 40) Then : gross = (40 + (hours - 40) * 1.5) * rate
    Else: gross = hours * rate
    End If

    gross = Utility.Round(gross, 2)
    Return Gross
    End Function 'CalculateGrossPay

    '---------------------------Function: CalculatePension---------------------------
    Function CalculatePension (ByVal hours As Decimal,
    ByVal payRate As Decimal) As Decimal

    Dim pension as Decimal

    If (hours > 30) Then : pension = 30 * payRate * 0.061
    Else: pension = hours * payRate * 0.061
    End If

    pension = Utility.Round(pension, 2)

    Return Pension
    End Function 'CalculatePension

    End Module 'P06

    Here is my result:
    Name:  Capture.jpg
Views: 364
Size:  32.5 KB

    The whole thing needs to be formatted to look like this and to line up like this:
    Name:  Capture 2.jpg
Views: 325
Size:  24.3 KB

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: Spacing help!

    Code:
    Console.Write(Space(4) & "{0:F2}", hours)
    The problem is "hours" can be different lenghts. The way I solve these problems is to space like this "Space(10 - hours.Length)", but this means "hours" would need to be a String. So, you need to know the maximum length of the variable your trying to format. In this case "hours" will never be longer than 6. So, you want to space 4 + 6 = Space(10 - hours.Length).

    this would apply to all the variables.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Spacing help!

    I've already shown you how to do this and you ignored the advice. If every number has two decimal places then the logical thing to do is simply right-align all the numbers and the decimal points will lineup automatically. The CodeBank link that I provided in a previous thread on this same subject shows how to do that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Spacing help!

    I would be using Console.SetCursonPostion for this I think. No on second thought, I would ditch the console app and use a datagridview.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width