need some help (currency)

boricua

New member
Joined
Aug 27, 2004
Messages
4
Programming Experience
Beginner
hello everyone, Im new here at this forum and also a newbie in VB.NET. I just started to program an assigment in which I need to calculate totals for students, credits and how much in that day students payed for courses. The thing is I did create the program, it runs ok, but when it comes to the money they have payed it should be in FormatCurrency so its displayed as money, when I put that code the program only shows the amount of money payed by the active students, not the total, but when I take that FormatCurrency, it works fine and it display, the amount of money payed by the active student plus the total of all the students registered. What am I doing wrong?? :confused:
Thanx for the help;)
 

Solitaire

Well-known member
Joined
Jun 28, 2004
Messages
465
Location
New York
Programming Experience
10+
You need to post your code so we can see what is going on.

(By the way, the correct spelling is, "paid." There is no such word as "payed" in the English language.)
 

boricua

New member
Joined
Aug 27, 2004
Messages
4
Programming Experience
Beginner
here u go:

TotalPayed = Val(txtTotalPayed.text)

thats the value of the total students then later on so it can add to the other students I put this one:

TotalPayed = TotalPayed + Quantity

which initially TotalPayed is 0 and Quantity is the amount of money the active student is paying
then to print that into the textbox i put

txtTotalPayed.text = FormatCurrency(TotalPayed, 2)
If I take that format out it will add all totals but with it, it doesnt work. u seem anything wrong? they are all Dim as Double cuz they hold money. plz if anything let me know thanx

and Im a latin and I did all that in spanish is no problem the spelling but thanx bro
 

boricua

New member
Joined
Aug 27, 2004
Messages
4
Programming Experience
Beginner
ok I already did it, thanx, and heres the code anyway, if u find anything wrong or that can be done better let me know, I wasnt using a class variable for the totals at the end and then I was giving a value to the variable with the formatCurrency which changed it to (0) no matter what it had before


VB.NET:
PublicClass Form1
 
Inherits System.Windows.Forms.Form
 
#Region " Windows Form Designer generated code "
 
PublicSubNew()
 
MyBase.New()
 
'This call is required by the Windows Form Designer.
 
InitializeComponent()
 
'Add any initialization after the InitializeComponent() call
 
EndSub
 
'Form overrides dispose to clean up the component list.
 
ProtectedOverloadsOverridesSub Dispose(ByVal disposing AsBoolean)
 
If disposing Then
 
IfNot (components IsNothing) Then
 
components.Dispose()
 
EndIf
 
EndIf
 
MyBase.Dispose(disposing)
 
EndSub
 
'Required by the Windows Form Designer
 
Private components As System.ComponentModel.IContainer
 
'NOTE: The following procedure is required by the Windows Form Designer
 
'It can be modified using the Windows Form Designer. 
 
'Do not modify it using the code editor.
 
FriendWithEvents lblDatos As System.Windows.Forms.Label
 
FriendWithEvents lblNumEst As System.Windows.Forms.Label
 
FriendWithEvents lblNombre As System.Windows.Forms.Label
 
FriendWithEvents lblCreditos As System.Windows.Forms.Label
 
FriendWithEvents lblFormaPago As System.Windows.Forms.Label
 
FriendWithEvents txtNumEst As System.Windows.Forms.TextBox
 
FriendWithEvents txtNombre As System.Windows.Forms.TextBox
 
FriendWithEvents txtCreditos As System.Windows.Forms.TextBox
 
FriendWithEvents txtCantidad As System.Windows.Forms.TextBox
 
FriendWithEvents cmdLimpiar As System.Windows.Forms.Button
 
FriendWithEvents cmdCalcular As System.Windows.Forms.Button
 
FriendWithEvents cmdTerminar As System.Windows.Forms.Button
 
FriendWithEvents txtFormaPago As System.Windows.Forms.TextBox
 
FriendWithEvents lblMetodo As System.Windows.Forms.Label
 
FriendWithEvents lblTotalEstudiantes As System.Windows.Forms.Label
 
FriendWithEvents ToolBar1 As System.Windows.Forms.ToolBar
 
FriendWithEvents lblCantidadPagada As System.Windows.Forms.Label
 
FriendWithEvents txtTotalPagado As System.Windows.Forms.TextBox
 
FriendWithEvents lblTotalCreditos As System.Windows.Forms.Label
 
FriendWithEvents txtTotalCreditos As System.Windows.Forms.TextBox
 
FriendWithEvents txtTotalEstudiantes As System.Windows.Forms.TextBox
 
<System.Diagnostics.DebuggerStepThrough()> PrivateSub InitializeComponent()
 
Me.lblDatos = New System.Windows.Forms.Label()
 
Me.lblNumEst = New System.Windows.Forms.Label()
 
Me.lblNombre = New System.Windows.Forms.Label()
 
Me.lblCreditos = New System.Windows.Forms.Label()
 
Me.lblFormaPago = New System.Windows.Forms.Label()
 
Me.txtNumEst = New System.Windows.Forms.TextBox()
 
Me.txtNombre = New System.Windows.Forms.TextBox()
 
Me.txtFormaPago = New System.Windows.Forms.TextBox()
 
Me.txtCreditos = New System.Windows.Forms.TextBox()
 
Me.txtCantidad = New System.Windows.Forms.TextBox()
 
Me.cmdLimpiar = New System.Windows.Forms.Button()
 
Me.cmdCalcular = New System.Windows.Forms.Button()
 
Me.cmdTerminar = New System.Windows.Forms.Button()
 
Me.lblMetodo = New System.Windows.Forms.Label()
 
Me.lblTotalEstudiantes = New System.Windows.Forms.Label()
 
Me.txtTotalEstudiantes = New System.Windows.Forms.TextBox()
 
Me.ToolBar1 = New System.Windows.Forms.ToolBar()
 
Me.lblCantidadPagada = New System.Windows.Forms.Label()
 
Me.txtTotalPagado = New System.Windows.Forms.TextBox()
 
Me.lblTotalCreditos = New System.Windows.Forms.Label()
 
Me.txtTotalCreditos = New System.Windows.Forms.TextBox()
 
Me.SuspendLayout()
 
'
 
'lblDatos
 
'
 
Me.lblDatos.Font = New System.Drawing.Font("Arial", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblDatos.Location = New System.Drawing.Point(16, 16)
 
Me.lblDatos.Name = "lblDatos"
 
Me.lblDatos.Size = New System.Drawing.Size(200, 23)
 
Me.lblDatos.TabIndex = 0
 
Me.lblDatos.Text = "Favor entrar los datos del estudiante:"
 
'
 
'lblNumEst
 
'
 
Me.lblNumEst.Font = New System.Drawing.Font("Arial", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblNumEst.Location = New System.Drawing.Point(16, 56)
 
Me.lblNumEst.Name = "lblNumEst"
 
Me.lblNumEst.Size = New System.Drawing.Size(56, 23)
 
Me.lblNumEst.TabIndex = 1
 
Me.lblNumEst.Text = "Num Est:"
 
Me.lblNumEst.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'lblNombre
 
'
 
Me.lblNombre.Font = New System.Drawing.Font("Arial", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblNombre.Location = New System.Drawing.Point(176, 56)
 
Me.lblNombre.Name = "lblNombre"
 
Me.lblNombre.Size = New System.Drawing.Size(56, 23)
 
Me.lblNombre.TabIndex = 2
 
Me.lblNombre.Text = "Nombre:"
 
Me.lblNombre.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'lblCreditos
 
'
 
Me.lblCreditos.Font = New System.Drawing.Font("Arial", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblCreditos.Location = New System.Drawing.Point(16, 96)
 
Me.lblCreditos.Name = "lblCreditos"
 
Me.lblCreditos.Size = New System.Drawing.Size(56, 23)
 
Me.lblCreditos.TabIndex = 3
 
Me.lblCreditos.Text = "Creditos:"
 
Me.lblCreditos.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'lblFormaPago
 
'
 
Me.lblFormaPago.Font = New System.Drawing.Font("Arial", 8.25!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblFormaPago.Location = New System.Drawing.Point(184, 96)
 
Me.lblFormaPago.Name = "lblFormaPago"
 
Me.lblFormaPago.Size = New System.Drawing.Size(48, 30)
 
Me.lblFormaPago.TabIndex = 4
 
Me.lblFormaPago.Text = "Forma Pago:"
 
Me.lblFormaPago.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'txtNumEst
 
'
 
Me.txtNumEst.Location = New System.Drawing.Point(80, 56)
 
Me.txtNumEst.Name = "txtNumEst"
 
Me.txtNumEst.Size = New System.Drawing.Size(72, 20)
 
Me.txtNumEst.TabIndex = 5
 
Me.txtNumEst.Text = ""
 
'
 
'txtNombre
 
'
 
Me.txtNombre.ForeColor = System.Drawing.SystemColors.WindowText
 
Me.txtNombre.Location = New System.Drawing.Point(240, 56)
 
Me.txtNombre.Name = "txtNombre"
 
Me.txtNombre.Size = New System.Drawing.Size(136, 20)
 
Me.txtNombre.TabIndex = 6
 
Me.txtNombre.Text = ""
 
'
 
'txtFormaPago
 
'
 
Me.txtFormaPago.AutoSize = False
 
Me.txtFormaPago.Location = New System.Drawing.Point(240, 96)
 
Me.txtFormaPago.Name = "txtFormaPago"
 
Me.txtFormaPago.Size = New System.Drawing.Size(24, 24)
 
Me.txtFormaPago.TabIndex = 7
 
Me.txtFormaPago.Text = ""
 
'
 
'txtCreditos
 
'
 
Me.txtCreditos.AutoSize = False
 
Me.txtCreditos.Location = New System.Drawing.Point(80, 96)
 
Me.txtCreditos.Name = "txtCreditos"
 
Me.txtCreditos.Size = New System.Drawing.Size(24, 23)
 
Me.txtCreditos.TabIndex = 8
 
Me.txtCreditos.Text = ""
 
'
 
'txtCantidad
 
'
 
Me.txtCantidad.AutoSize = False
 
Me.txtCantidad.BackColor = System.Drawing.SystemColors.InactiveBorder
 
Me.txtCantidad.Location = New System.Drawing.Point(16, 144)
 
Me.txtCantidad.Name = "txtCantidad"
 
Me.txtCantidad.Size = New System.Drawing.Size(272, 40)
 
Me.txtCantidad.TabIndex = 9
 
Me.txtCantidad.Text = ""
 
'
 
'cmdLimpiar
 
'
 
Me.cmdLimpiar.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.cmdLimpiar.Location = New System.Drawing.Point(32, 296)
 
Me.cmdLimpiar.Name = "cmdLimpiar"
 
Me.cmdLimpiar.Size = New System.Drawing.Size(64, 32)
 
Me.cmdLimpiar.TabIndex = 10
 
Me.cmdLimpiar.Text = "&Limpiar"
 
'
 
'cmdCalcular
 
'
 
Me.cmdCalcular.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.cmdCalcular.Location = New System.Drawing.Point(128, 296)
 
Me.cmdCalcular.Name = "cmdCalcular"
 
Me.cmdCalcular.Size = New System.Drawing.Size(64, 32)
 
Me.cmdCalcular.TabIndex = 11
 
Me.cmdCalcular.Text = "&Calcular"
 
'
 
'cmdTerminar
 
'
 
Me.cmdTerminar.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.cmdTerminar.Location = New System.Drawing.Point(224, 296)
 
Me.cmdTerminar.Name = "cmdTerminar"
 
Me.cmdTerminar.Size = New System.Drawing.Size(64, 32)
 
Me.cmdTerminar.TabIndex = 12
 
Me.cmdTerminar.Text = "&Terminar"
 
'
 
'lblMetodo
 
'
 
Me.lblMetodo.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblMetodo.Location = New System.Drawing.Point(272, 88)
 
Me.lblMetodo.Name = "lblMetodo"
 
Me.lblMetodo.Size = New System.Drawing.Size(112, 40)
 
Me.lblMetodo.TabIndex = 13
 
Me.lblMetodo.Text = "(E= efectivo, B=beca, V=veterano, O=otro)"
 
'
 
'lblTotalEstudiantes
 
'
 
Me.lblTotalEstudiantes.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblTotalEstudiantes.Location = New System.Drawing.Point(16, 208)
 
Me.lblTotalEstudiantes.Name = "lblTotalEstudiantes"
 
Me.lblTotalEstudiantes.Size = New System.Drawing.Size(72, 24)
 
Me.lblTotalEstudiantes.TabIndex = 14
 
Me.lblTotalEstudiantes.Text = "Total De Estudiantes:"
 
Me.lblTotalEstudiantes.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'txtTotalEstudiantes
 
'
 
Me.txtTotalEstudiantes.Location = New System.Drawing.Point(96, 208)
 
Me.txtTotalEstudiantes.Name = "txtTotalEstudiantes"
 
Me.txtTotalEstudiantes.Size = New System.Drawing.Size(40, 20)
 
Me.txtTotalEstudiantes.TabIndex = 15
 
Me.txtTotalEstudiantes.Text = ""
 
'
 
'ToolBar1
 
'
 
Me.ToolBar1.DropDownArrows = True
 
Me.ToolBar1.Name = "ToolBar1"
 
Me.ToolBar1.ShowToolTips = True
 
Me.ToolBar1.Size = New System.Drawing.Size(424, 39)
 
Me.ToolBar1.TabIndex = 16
 
'
 
'lblCantidadPagada
 
'
 
Me.lblCantidadPagada.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblCantidadPagada.Location = New System.Drawing.Point(168, 208)
 
Me.lblCantidadPagada.Name = "lblCantidadPagada"
 
Me.lblCantidadPagada.Size = New System.Drawing.Size(64, 24)
 
Me.lblCantidadPagada.TabIndex = 17
 
Me.lblCantidadPagada.Text = "Cantidad Pagada:"
 
Me.lblCantidadPagada.TextAlign = System.Drawing.ContentAlignment.BottomRight
 
'
 
'txtTotalPagado
 
'
 
Me.txtTotalPagado.Location = New System.Drawing.Point(240, 208)
 
Me.txtTotalPagado.Name = "txtTotalPagado"
 
Me.txtTotalPagado.Size = New System.Drawing.Size(88, 20)
 
Me.txtTotalPagado.TabIndex = 18
 
Me.txtTotalPagado.Text = ""
 
'
 
'lblTotalCreditos
 
'
 
Me.lblTotalCreditos.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.lblTotalCreditos.Location = New System.Drawing.Point(16, 248)
 
Me.lblTotalCreditos.Name = "lblTotalCreditos"
 
Me.lblTotalCreditos.Size = New System.Drawing.Size(72, 24)
 
Me.lblTotalCreditos.TabIndex = 19
 
Me.lblTotalCreditos.Text = "Total Creditos"
 
Me.lblTotalCreditos.TextAlign = System.Drawing.ContentAlignment.MiddleRight
 
'
 
'txtTotalCreditos
 
'
 
Me.txtTotalCreditos.Location = New System.Drawing.Point(96, 248)
 
Me.txtTotalCreditos.Name = "txtTotalCreditos"
 
Me.txtTotalCreditos.Size = New System.Drawing.Size(40, 20)
 
Me.txtTotalCreditos.TabIndex = 20
 
Me.txtTotalCreditos.Text = ""
 
'
 
'Form1
 
'
 
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
 
Me.ClientSize = New System.Drawing.Size(424, 342)
 
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtTotalCreditos, Me.lblTotalCreditos, Me.txtTotalPagado, Me.lblCantidadPagada, Me.ToolBar1, Me.txtTotalEstudiantes, Me.lblTotalEstudiantes, Me.lblMetodo, Me.cmdTerminar, Me.cmdCalcular, Me.cmdLimpiar, Me.txtCantidad, Me.txtCreditos, Me.txtFormaPago, Me.txtNombre, Me.txtNumEst, Me.lblFormaPago, Me.lblCreditos, Me.lblNombre, Me.lblNumEst, Me.lblDatos})
 
Me.Name = "Form1"
 
Me.Text = "Matricula"
 
Me.ResumeLayout(False)
 
EndSub
 
#EndRegion
 
'Programa para Calcular la cantidad a pagar por concepto de matricula, ademas de acumular los datos por estudiantes para cada valor
 
'Declaracion de "class variables" para la acumulacion de los datos
 
Dim TotalPagado AsDouble
 
Dim TotalCreditos AsDouble
 
Dim n AsInteger
 
PrivateSub cmdCalcular_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalcular.Click
 
'Obtener los valores del estudiante
 
Dim numEst, nombre AsString
 
Dim Dinero AsDouble
 
Dim creditos AsSingle
 
Dim formaPago AsString
 
Dim cantidad AsSingle
 
numEst = txtNumEst.Text
 
nombre = txtNombre.Text
 
creditos = Val(txtCreditos.Text)
 
formaPago = txtFormaPago.Text
 
n = Val(txtTotalEstudiantes.Text)
 
TotalCreditos = Val(txtTotalCreditos.Text)
 
'Calcular la cantidad a pagar
 
If UCase(formaPago) = "E" Then
 
If creditos >= 12 Then
 
cantidad = 100 + 90 * creditos
 
Else
 
cantidad = 200 + 90 * creditos
 
EndIf
 
Else
 
If UCase(formaPago) = "B" Then
 
cantidad = 200
 
Else
 
If creditos >= 12 Then
 
cantidad = 100 + 70 * creditos
 
Else
 
cantidad = 200 + 70 * creditos
 
EndIf
 
EndIf
 
EndIf
 
'Funciones para la acumulacion de datos a ser desplegados mas adelante
 
n = n + 1
 
TotalCreditos = TotalCreditos + creditos
 
TotalPagado = TotalPagado + cantidad
 
'Desplegar la cantidad a pagar y los totales para cada uno de los campos
 
txtCantidad.Text = "La cantidad a pagar por " & nombre & " es " & FormatCurrency(cantidad, 2)
 
txtTotalEstudiantes.Text = n
 
txtTotalCreditos.Text = TotalCreditos
 
txtTotalPagado.Text = FormatCurrency(TotalPagado, 2)
 
EndSub
 
PrivateSub cmdLimpiar_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles cmdLimpiar.Click
 
'Limpiar los objetos antes de entrar nuevos datos
 
txtNumEst.Text = ""
 
txtNombre.Text = ""
 
txtCreditos.Text = ""
 
txtFormaPago.Text = ""
 
txtCantidad.Text = ""
 
EndSub
 
PrivateSub cmdTerminar_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles cmdTerminar.Click
 
'Terminar el programa
 
End
 
EndSub
 
EndClass

 
Last edited by a moderator:

mathpath

Active member
Joined
Aug 21, 2004
Messages
27
Programming Experience
10+
Hi Boricua,

Your problem may be very simply solved by making change to one single line of code:

TotalCreditos = Val(txtTotalCreditos.Text.Substring(1)) 'if you use FormatCurrency

The reason for that is you are trying to evaluate value of the TEXT of the textbox, which displays the $ sign at the beginning (for American locale), and which consequently returns a value of zero. The above code works for the American and Canadian locales which use a $ sign before the dollar amount. For other locals, you'd have to adjust accordingly.

The substring(1) following the text will extract the substring starting from character 1 (character 0 is the first), namely the second character, skipping the dollar sign.

Please try it out and write to us again if you still have problems.

Happy Programming.

P.S. Next time you post code, you only have to post the part after the
#END REGION ...
line. The many lines of .net generated code might have scared away many people willing and able to help you.
 

boricua

New member
Joined
Aug 27, 2004
Messages
4
Programming Experience
Beginner
well it gave an error that the lenght cant be less than zero, I just toot out that TotalCreditos = Val(txtTotalCreditos.Text) and declared that variable TotalCreditos as a class variable and there I did it
Thanx all for the help
see u next time
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
I am working on a project at the moment that sets and reads currency values to and from text boxes. When reading, I use Decimal.Parse(TextBox.Text, Globalization.NumberStyles.Currency). This is basically the inverse of using FormatCurrency(). I think this is a better method than removing the first character as it will work for locales that do not put the currency symbol at the front of the value.

By the way: the second argument to FormatCurrency() is unnecessary unless you want use something other than the default. The client machine's Regional Settings will handle the number of decimal places if this argument is not defined.
 
Top Bottom