Decimals wont show

  • Thread starter Thread starter WStoreyII
  • Start date Start date
W

WStoreyII

i have a decimatl decsum and decqty
the values are 456 and 229

when i divide these two numbers and try to display teh results in a text box
i get 228 when i need 228.044
i have tried to fromat

"#0.0000" but alls this does is give me

228.0000

what do i need to do to fix this ?

WStoreyII
 
* "WStoreyII said:
i have a decimatl decsum and decqty
the values are 456 and 229

when i divide these two numbers and try to display teh results in a text box
i get 228 when i need 228.044
i have tried to fromat

"#0.0000" but alls this does is give me

228.0000

what do i need to do to fix this ?

Post the complete code you used for formatting. Are you sure you
assigned the result of the calculation to a variable which supports
decimals after the comma ('Single', 'Double')?
 
Here is the code
it was not assigned to a double but i tried that and it also did not work
here is the original code

WStoreyII

Private Sub FrmLab4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnCompute.Enabled = False ' Disables the computer button
End Sub
Private Sub txtNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
Dim KeyPressed As String = UCase(e.KeyChar.ToString) ' Assigns Character of Key Pressed to Variable
If KeyPressed Like "[!0-9]" Then ' If Key is not a number then delete text
txtNumber.Clear()
Else
BtnCompute.Enabled = True 'Enables button once valid text is entered
End If
End Sub
Private Sub BtnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCompute.Click
Dim IntNumber As Integer = Val(txtNumber.Text) ' Assigns Text To Variable
If IntNumber >= 102 AndAlso IntNumber <= 676 Then
LblAverage.Text = Format(Average(IntNumber), "")
Else
Select Case IntNumber
Case Is < 102
MsgBox("Number is to Low", MsgBoxStyle.OKOnly, "Low Number!")
Case Is > 676
MsgBox("Number is to High", MsgBoxStyle.OKOnly, "High Number!")
End Select
End If
End Sub
Public Function Average(ByVal X As Integer) As Integer
Dim IntSum As Decimal = 1 ' Sum of All Even Number between 1 and x plus 1 and x if x not even
Dim DecAverage As Decimal ' Average
Dim IntQty As Decimal = 1 ' Number of even numbers between 1 and x plus 1 and x if x not even
Dim DecOdd As Decimal ' Check For odd number in x
DecOdd = X Mod 2
If DecOdd = 0 Then
For A As Integer = 2 To X Step 2
IntSum += A
IntQty += 1
Next
Else
IntSum += X
IntQty += 1
For B As Integer = 2 To X - 1 Step 2
IntSum += B
IntQty += 1
Next
End If
DecAverage = IntSum / IntQty
Return DecAverage
End Function
Private Sub BtnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
 
Your 'Average' function is returning an Integer so the decimal value you are returning is converted to an integer. Change your return type to a 'Decimal'...

\\\
Public Function Average(ByVal X As Integer) As Decimal
///


HTH,
Gary

Here is the code
it was not assigned to a double but i tried that and it also did not work
here is the original code

WStoreyII

Private Sub FrmLab4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BtnCompute.Enabled = False ' Disables the computer button
End Sub
Private Sub txtNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
Dim KeyPressed As String = UCase(e.KeyChar.ToString) ' Assigns Character of Key Pressed to Variable
If KeyPressed Like "[!0-9]" Then ' If Key is not a number then delete text
txtNumber.Clear()
Else
BtnCompute.Enabled = True 'Enables button once valid text is entered
End If
End Sub
Private Sub BtnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCompute.Click
Dim IntNumber As Integer = Val(txtNumber.Text) ' Assigns Text To Variable
If IntNumber >= 102 AndAlso IntNumber <= 676 Then
LblAverage.Text = Format(Average(IntNumber), "")
Else
Select Case IntNumber
Case Is < 102
MsgBox("Number is to Low", MsgBoxStyle.OKOnly, "Low Number!")
Case Is > 676
MsgBox("Number is to High", MsgBoxStyle.OKOnly, "High Number!")
End Select
End If
End Sub
Public Function Average(ByVal X As Integer) As Integer
Dim IntSum As Decimal = 1 ' Sum of All Even Number between 1 and x plus 1 and x if x not even
Dim DecAverage As Decimal ' Average
Dim IntQty As Decimal = 1 ' Number of even numbers between 1 and x plus 1 and x if x not even
Dim DecOdd As Decimal ' Check For odd number in x
DecOdd = X Mod 2
If DecOdd = 0 Then
For A As Integer = 2 To X Step 2
IntSum += A
IntQty += 1
Next
Else
IntSum += X
IntQty += 1
For B As Integer = 2 To X - 1 Step 2
IntSum += B
IntQty += 1
Next
End If
DecAverage = IntSum / IntQty
Return DecAverage
End Function
Private Sub BtnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
 
Back
Top