How to convert the unit like Dozen to Pcs in vb.net - vb.net-2010

How do I convert the Unit in vb.net like below sample.
12 Pcs=1 Dozen
If I Put 14 Pcs into textbox1.text it will reply in a label box like below. Please help me.
14 Pcs- Textbox1.Text
1 Dozen 2 Pcs- Label1.text

Try this
Dim Quantity As Integer
Dim Dozens As Integer
Dim Pcs As Integer
Int32.TryParse(txtPcs.Text, Quantity)
Dozens = Quantity / 12
Pcs = (Quantity - (Dozens * 12)) Mod 12

Related

How do I make my percentages have 2 decimal points?

I have looked at other posts about this but i just can't get my head around it and wanted to ask specifically for my scenario. I have written a Visual Basic console application and I am trying to get my percentages to have two decimal places on them as they're just rounding up to the nearest whole number. Here is the code :
Console.Write("Enter the percentage you want to work out: ")
Dim Percentage As Integer
Percentage = Console.ReadLine()
Console.Write("What would you like to work out " & Percentage & "% of? ")
Dim Number As Integer
Number = Console.ReadLine()
Dim PercentageNumberResult As Integer
PercentageNumberResult = Number / 100 * Percentage
Console.Write(Number & " ÷ 100 × " & Percentage & " = " & PercentageNumberResult)
Console.ReadLine()
Any help would be appreciated, thanks!
The problem is due to PercentageNumberResult being an integer.
Make PercentageNumberResult a double :
Dim PercentageNumberResult As Double
PercentageNumberResult = Number / 100 * Percentage
You could then use Math.Round if needed. Also you should turn on Option Strict this will help solving the problem.
If you want to convert a number to a String then you call ToString on that number. If you want a specific format then you pass the appropriate format specifier as an argument when calling ToString. You should read the relevant documentation to learn all the possible format specifiers.
In this case, you display a number as a percentage using the "p" or "P" specifier and you can add a number to specify the number of decimal places. e.g.
Dim x = 13
Dim y = 1000
Dim z = x / y
Dim s = z.ToString("P2")
After executing that code, s will contain "1.30%".
If you don't actually want the "%" symbol in the output then you can multiply by 100 and use a different format specifier, e.g.
Dim x = 13
Dim y = 1000
Dim z = 100 * x / y
Dim s = z.ToString("N2")
After executing that code, s will contain "1.30".

Visual Basic- Random Numbers

I'm trying to generate 5 random numbers from 1-99 and display them in a ListBox. Can someone tell me where I'm going wrong? Right now my code is displaying all 99 numbers in the ListBox, but I only want 5 of them to display. Here is the code:
'list to store numbers
Dim numbers As New List(Of Integer)
'add desired numbers to list
For count As Integer = 1 To 99
numbers.Add(count)
Next
Dim Rnd As New Random
Dim SB As New System.Text.StringBuilder
Dim Temp As Integer
'select a random number from the list, add to listbox and remove it so it can't be selected again
For count As Integer = 0 To numbers.Count - 1
Temp = Rnd.Next(0, numbers.Count)
SB.Append(numbers(Temp) & " ")
ListBox2.Items.Add(numbers(Temp))
numbers.RemoveAt(Temp)
Next
Replace
For count As Integer = 0 To numbers.Count - 1
With
For count As Integer = 1 To 5
The above will work but, You need to add count just after your next statement as well. I recommend checking into learning more about loops as well. Clearly Visual Basic 2012 is great for that.

Overflow when processing historical price data (VBA)

I'm writing a macro that reads price data from an excel file, write them on another excel file and does basic calculations (e.g. returns). The data is made of 2600 x 100 data points.
When I run it i have an overflow error! looks like it's handling too much data. Is it an issue with the computer's memory? (i have a modern laptop with 4GB Ram) or does it have to do with the way the data is stored during the calulations? What I don't get is that if I try to do that kind of calculations in the spreadsheet directly, i won't have an overflow message.
Btw, I'm trying to avoid using Access.
Thanks for your help.
Sam
Dim i As Long
Dim j As Long
Dim k As Long
Dim m As Long
Dim n As Long
Dim o As Long
' FYI yearsconsidered = 10
For j = 1 To Group(k).TickerCount
For m = 1 To Quotes(j, k).ContinuationCount
For i = 1 To YearsConsidered * 261
If Sheets(j).Cells(i - GivenPeriod, 4 * m - 3) = 0 Then
Sheets(j).Cells(i, 4 * m - 2) = 0
Else
Sheets(j).Cells(i, 4 * m - 2) = (Sheets(j).Cells(i, 4 * m - 3) - Sheets(j).Cells(i - GivenPeriod, 4 * m - 3)) / Sheets(j).Cells(i - GivenPeriod, 4 * m - 3)
End If
Next i
Next m
Next j
I have experienced a similar situation when I'm looping through a large number of data points. For some reason, this does not happen if I code the formula entry into a range of cells (at the same time); like when you manually select a range in the spreadsheet, enter the formula once, then hit Ctrl+Enter. This will require for the proper use of the $ signs (absolute vs relational positions).
It's hard to tell exactly what the issue is without seeing the code, so not sure if this is your problem...

Increment the last digit of a number

VB2010 I have a user form where the user inputs a number format. The routine then cycles through a list of number pairs and displays them in a list of categories:
User format "0.00"
0.00 - 164.04
164.04 - 410.10
410.10 - 820.21
What I am trying to do is to increment the first value by one digit so there is no overlap. something like:
0.00 - 164.04
164.05 - 410.10
410.11 - 820.21
I am trying to make it so it works with any number format the user inputs like "0.000" or "0.0". What I currently have is (example for value 164.04)
1. Convert the value to a string "164.04"
2. Take the right most character "4" and convert to an integer 4
3. Increment the integer value by 1 to get 5
4. Take the characters in the string from step #1 except the last and then append
the integer from Step #3 as a string to get "164.05".
Seemed to work in my VB6 program but wanted to see if anyone had any better ideas. I also don't think i accounted for the last digit being a 9.
Update: based on the suggestions below what ended up working for positive and negative numbers and integers and floats was the following:
Dim p As Integer
Dim numAsStr As String = num.ToString(fmt)
If numAsStr.IndexOf(".") = -1 Then
p = 0
Else
p = numAsStr.Length - numAsStr.IndexOf(".") - 1
End If
Dim result as Double = ((num* (10 ^ p) + 1.0) / (10 ^ p))
Here is the algorithm:
1.Find decimal points (p)
2.multiply the number by 10^p, increase it by one, divide it back by 10^p
Dim numAsStr As String = num.ToString()
Dim p As Integer = numAsStr.Length - numAsStr.IndexOf(".") - 1
Dim numInt as Integer = 10^p * num
Dim result as Double = ((10^p *num + 1.0) / 10^p).ToString()
Use "ON ERROR RESUME NEXT" construct for this problem.

VB - Divide integers doesn't include commas?

This is probably an easy one, but I've got this Excel vba macro where I'm trying to divide two integers and I end up with a round number, even though there should be a comma in there.
For example 278 / 101 = 3. While it's really 2,75 and not 3. Why is this?
The macro is really simple, like this:
Dim intFull, intLow , intDivided as Integer
intFull = 278
intLow = 101
intDivided = intFull \ intLow
Your result variables is an integer
If you work with doubles instead you will get 2.752 etc - which can be rounded using dbDivided = Round(lngFull / lngLow, 2)
[variables updated to be more meaningful]
Sub redone()
Dim lngFull As Long
Dim lngLow As Long
Dim dbDivided As Double
lngFull = 278
lngLow = 101
dbDivided = Round(lngFull / lngLow, 2)
End Sub
Sure you used a forward slash and not a backslash? (See also: http://zo-d.com/blog/archives/programming/vba-integer-division-and-mod.html)