So for example I need to half the number 43 to an integer, however when I do this it rounds the 21.5 made to 22 when I want it to be rounded down to 21. How do I do this? I'm using VB.net
Did you try Math.Truncate(43.0 / 2.0)? This will divide by two, and then drop any fraction, leaving only the integer part of the division.
Well rounding to 23 is correct... mathematically, however, if you want to round down:
Example: (WPF)
Dim a As Integer = 43
Dim b As Integer = CInt(Math.Floor(a / 2))
Label1.Content = b
Example: (WinForms)
Dim a As Integer = 43
Dim b As Integer = CInt(Math.Floor(a / 2))
Label1.Text= b
Another way is 43 >> 1 (this is a logical right shift - the fastest method). 43 \ 2 (this is an integer divide - second fastest).
Related
If I had a integer that was, for example 11110011. How would I find out what number was in each position so I could use them calculate something. Thanks
cast it to String and then use method of string to find the int at a particular position.
Feel free to read a bit about operators if you wish.
Anyhow i'd use a loop of Mod 10 and divide by 10 each iteration.
Pseudo code:
Given number
While number is not zero
Get the mod 10 of the number
... Do what you need to do with it - that's the digit
Divide by 10.
Repeat.
Done.
for example:
Dim yourNumber As Integer = 12930 // for example.
While index >0
Dim digit As Integer = yourNumber Mod 10
Debug.Write(digit.ToString & " ")
index /= 1
End While
Will write all the digits, seperated by a space:
output: 1 2 3 9 0
I have a calculation like this: 3 * 12300 / 160. The result is: 230.625. But I just want the integer part, 230.
In C, this can be done using something like this: int MyVar = (int)3*12300/160;
Is there a way in VBA (With MS-Access) for force the result to be an integer?
You can round down using the Int or Fix functions.
Since you know the result you want is a whole number, you should store the result in a variable of type Long (or Integer if you're absolutely certain it will always be smaller than 32768).
Dim l As Long
l = Int(3 / 160 * 12300) ' <~~~~ Yes, I switched the numbers around on purpose!*
MsgBox "l = " & l
* Why did I switch the numbers around? Because the expression 3 * 12300 / 160 will throw an error in VBA. Read here why: Overflow when multiplying Integers and assigning to Long
I have an assignment for a beginners VB class, and I have looked for examples for a penny calculator and have read them and tried to figure out where I am going wrong. I have a text box that takes the number of pennies you want to figure out. If you input 101 pennies it comes back with 1 dollar and 1 penny.
Strangely it works for up to 137 pennies. That comes back with 1 dollar 1 quarter 1 dime 2 pennies. If it goes to 138 or higher it just screws up. If I input 138 I get 1 dollar, 2 quarters, 1 dime, 1 nickel, 3 pennies. If I use 17 I get 1 quarter, 2 dimes, 1 nickel, 2 pennies.
Here is the arithmetic portion of my code.
DolBack = intLeftOver / 100
intLeftOver = intLeftOver Mod 100
LblDolRes.Text = DolBack.ToString
QrtBack = intLeftOver / 25
intLeftOver = intLeftOver Mod 25
LblQrtRes.Text = QrtBack.ToString
DimBack = intLeftOver / 10
intLeftOver = intLeftOver Mod 10
LblDimRes.Text = DimBack.ToString
NicBack = intLeftOver / 5
intLeftOver = intLeftOver Mod 5
LblNicRes.Text = NicBack.ToString
PenBack = intLeftOver
LblPenRes.Text = PenBack.ToString
I have tried to look my code over and look at other examples, but apparently I'm doing it a little differently. If anyone could point out the apparent major flaw in my code or my way of doing it I would appreciate it.
Further clarifying I have looked at the post at
Penny Calculator
Obviously there are a few differences with the actual arithmetic. In the link given is that because that is the only way to do it without rounding issues?
there are 100 pennies in a dollar, 25 pennies in a quarter, 10 pennies in a dime, 5 pennies in a nickel.
EDIT: Thanks for the help and pointing out my error. It's appreciated.
You're right that it's a rounding error. Use
DolBack = intLeftOver \ 100
instead of
DolBack = intLeftOver / 100
(and the same for the other ones) and you'll see a very different result.
The reason is that the / operator will do floating point division, regardless of the values either side of it, so 38/25 will yield an answer of 1.52, which rounds upwards (as you suspected) when assigned back to the variable, which is defined as an Integer. On the other hand, the \ operator will do integer division, truncating rather than rounding.
The Math.DivRem method is helpful for this kind of problem.
Dim dollars As Integer
Dim fifty As Integer
Dim quarter As Integer
Dim dime As Integer
Dim nickel As Integer
Dim pennies As Integer = 137
dollars = Math.DivRem(pennies, 100, pennies)
fifty = Math.DivRem(pennies, 50, pennies)
quarter = Math.DivRem(pennies, 25, pennies)
dime = Math.DivRem(pennies, 10, pennies)
nickel = Math.DivRem(pennies, 5, pennies)
In vb.net, how to convert a 32 bit integer to 2 shorts? and reconstruct them?
The first 16 bit will go to the first value, the last 16 bits will go to the second value
You can use the BitConverter class.
dim i16a, i16b as short
dim i32 as integer = somevalue
dim b() as byte
b = BitConverter.GetBytes(i32)
i16a = BitConverter.ToInt16(b, 0)
i16b = BitConverter.ToInt16(b, 2)
You can reconstruct the same way, just copy the 4 bytes into a single array first before using BitConverter.ToInt32.
I'd like to generate random numbers in a range (say between 0 and 1) - but with a certain step size (say 0.05).
There's a python function that does exactly that:
random.randrange ( [start,] stop [,step] )
So my problem is not how to generate random numbers in a range - instead I need a way to do this with a certain step size.
How can this be done in .NET?
You could generate a random integer between 0 and 20 and divide the result by 20
Dim rnd = New Random()
Dim nextValue = rnd.Next(21) / 20
This will give you a random number between 0 and 1 (inclusive) in 0.05 increments
You can try something like that:
Dim objRandom As New System.Random
Label1.Text = Math.Round(objRandom.NextDouble() * 2, 1) / 2
So you create a random double and you round it to one digit (example: 0.8).
Then you divided it with 2 and you get what you want