dividing number without rounding it up and without decimal - vb.net

I'm trying to divide input number and show the result without decimals and without rounding it up, so if I divide 80/50 I want to get 1, not 2 (1.6).
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim box1 As Decimal = TextBox1.Text()
TextBox6.Text = box1 / 50
End Sub
End Class

You can use Truncate Method of Decimal:
Dim result As Decimal = Decimal.Truncate(CDec(80 / 50))
Console.WriteLine("result : " & result.ToString)

You can use the \ operator to do integer division.
Dim result = 80 \ 50 'result is Integer with value 1

Related

Does anybody know how to generate a set of random numbers in vb.net between two numbers that follows Poisson distribution?

The numbers should be discrete and have an upper bound and lower bound.
I am using the probability distribution formula of poison distribution and using it in my code but I am not getting the result.
i don't know exactly what you mean by follows Poisson distribution? but if you want to generate random values in vb.net between two numbers you could use something like so
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim LowerBound As Integer = 15
Dim UpperBound As Integer = 30
Dim Result As Integer = CInt(LowerBound + (Rnd() * ((UpperBound + 1) - LowerBound)))
TextBox1.Text = Result
End Sub
and the updated version from Rnd() is the Random().Next instruction as Hursey commented which can be used like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim LowerBound As Integer = 15
Dim UpperBound As Integer = 30
' Instantiate random number generator using system-supplied value as seed.
Dim rand As New Random()
Dim Result As Integer = rand.Next(LowerBound, UpperBound)
rand = Nothing
TextBox1.Text = Result
End Sub

When someone types in a word with 2-20 characters in into txtusername I want progressbar1 to =100

I can make the progress bar equal 100 if someone enters a specific word into txtUsername by writing:
Dim word1 As String = "14TSmith"
If txtUsername = word1 Then
progressbar1.value = 100
End If
(sorry I can't copy and paste the exact code because it is on another system)
But I want the user to type in any string that is between 2-20 characters long, not just a specific word.
If txtusername.TextLength >= 2 And txtusername.TextLength <= 20 Then
ProgressBar1.Value = 100
End If
I hope this helps.
If you want to give the user feedback of how "good" their password is based on the length, then:
Public Class Form1
Private Const MinLength As Integer = 2
Private Const MaxLength As Integer = 20
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.MaxLength = MaxLength
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim Percent As Double
If TextBox1.TextLength < MinLength Then
Percent = 0
Else
Percent = CDbl(TextBox1.TextLength) / CDbl(MaxLength)
End If
Dim value As Integer = ProgressBar1.Minimum + (ProgressBar1.Maximum - ProgressBar1.Minimum) * Percent
ProgressBar1.Value = Math.Max(Math.Min(value, ProgressBar1.Maximum), ProgressBar1.Minimum)
End Sub
End Class

Vb Write a program to print multiples of 2 and 3

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.
I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub
This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

Trying to format textbox value in a something like money

I'm trying to show on a textbox a value like money exemple: 10.50. But everytime i put 10.50 its show 10.5
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button1.Click
textbox3.text = val(textbox1.text) + val(textbox2.text)
Use format string.
textbox3.text = (val(textbox1.text) + val(textbox2.text)).ToString("N")
I'd like to suggest Decimal.TryParse method instead of val() function.
Dim no1,no2,no3 as Decimal
Decimal.TryParse(TextBox1.Text,no1)
Decimal.TryParse(TextBox2.Text,no2)
no3= no1 + no2
TextBox3.Text= no3.ToString("N")
So why don't you just use an NumericUpDown instead?
You can set it to have two decimal places using
NumericUpDownCtrlName.DecimalPlaces = 2
To get or set the value which is a decimal simply use:
NumericUpDownCtrlName.Value

Calculator for newbs

I'm working on a calculator in which I want to get some numbers on a circle.
Private Sub Button6_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button6.Click
Dim radius As Integer = TextBox13.Text
Dim diameter As Integer = TextBox14.Text
Dim length As Integer = TextBox15.Text
TextBox13.Text = diameter / 2
TextBox14.Text = radius * 2
TextBox15.Text = radius * 2 * Math.PI
TextBox15.Text = diameter * Math.PI
End Sub
That is the current code, but I'm experiencing a problem with "the number must be less than infinity".
Note: I'm a COMPLETE noob.
The error lies in the fact that you tried to assign an integer value of type string.
Use the method provided by struct TryParse integer, this will also run in any runtime exception FormatException bran and do not send the application.
Here's an example:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim diameter As Integer = 0
Dim radius As Integer = 0
Dim lenght As Integer = 0
If Integer.TryParse(Me.TextBox13.Text, diameter) Then
'your code
End If
If Integer.TryParse(Me.TextBox14.Text, radius) Then
'your code
End If
If Integer.TryParse(Me.TextBox15.Text, lenght) Then
'your code
End If
End Sub
More information about TryParse at this link:
http://msdn.microsoft.com/it-it/library/f02979c7.aspx
Bye
See if this works:
Dim radius As Integer = Integer.Parse(TextBox13.Text)
Dim diameter As Integer = Integer.Parse(TextBox14.Text)
Dim length As Integer = Integer.Parse(TextBox15.Text)