VB.Net -" console.write(Format((87.20 \ 43.60))) " returning result of 1 not 2, why? - vb.net

The below line of code is returning as a 1 instead of a 2, for reasons I can't comprehend.
console.write(Format((87.20 \ 43.60)))
Surely this should return the result of 2 but I've checked in another environment and it returns a can anyone tell me why?
I have tried putting the code into a second environment but the result was the same I don't understand why it is returning 1 instead of 2, can anyone enlighten me?

Thanks for the help but found the answer.
Decimals are converted to Long before Integer division and due to this are subject to banker's rounding, multiplying both numbers by a 100 before the operation resolves the issue.
Source of information:
Learn Microsoft - VB.Net - \ Operator - Remarks
Thanks,

While I am glad you resolved your own question, I did want to provide an alternative.
When you use the integer division operator, you do not have control as to how the rounding should occur. Whereas if you do a floating-point division operator you can keep the precision and then use one of the built-in Math class methods (documentation) to specify how the number should round.
Take a look at this example:
Dim result = 87.20 / 43.60
Dim roundUp = Math.Ceiling(result)
Dim roundDown = Math.Floor(result)
Dim bankersRounding = Math.Round(result)
Fiddle: https://dotnetfiddle.net/LZEMXV
Because in your example you are using Console.Write (which treats the data as a String) you do not need to cast the value to an Integer data type. However, if you needed the value as an Integer, any one of those variables outside result can be safely converted to an Integer using the Parse method (documentation).

Related

Visual Basic - How to check how many times a number goes into another

I need to check how many sixes can fit into a number entered by the user to be able to use it in my program later on. So for example, two sixes can fit into 13. I was wondering if there's an algorithm I can use for this.
You want to do an integer division. See the MSDN page about arithmetic operators or the MSDN page about the \ Operator:
Dim k As Integer
k = 23 \ 5
' The preceding statement sets k to 4.
if you need that, simply divide one by the other and change to integer:
https://msdn.microsoft.com/en-us/library/se0w9esz.aspx says
number1 \ number2
returns the value you desire for VB, I presume it carries over to VBA.
Otherwise, you can use
CInt(Fix(number1/number2))
(from https://msdn.microsoft.com/en-us/library/xh29swte(v=vs.90).aspx)

CInt vs. Math.Round in Visual Basic .NET

What is the difference between:
Dim a As Integer = CInt(2.2)
and
Dim a As Integer = Math.Round(2.2)
?
CInt returns an integer but will round the .5 to nearest even number so:
2 = CInt(2.5)
4 = CInt(3.5)
Are both true, which might not be what you want.
Math.Round can be told to round away from zero. But returns a double, so we still need to cast it
3 = CInt(Math.Round(2.5, MidpointRounding.AwayFromZero))
There is bigger differences in CInt(), Int() and Round()... and others.
Round has parameters of rounding, so it is flexible and user friendly. But it do not change variable type. No "type conversion".
Meanwhile CInt() is a bit cryptic as it rounds too. And it is doing "Type conversion" to integer.
2 = Int(2.555), 3 = CInt(2.555)
2 = Int(2.5), 2 = CInt(2.5)
Some documentation states:
When the fractional part of expression is exactly .5, CInt always rounds it to the nearest even number. For example, .5 rounds to 0, and 1.5 rounds to 2.
But I do not like that "exact 0.5", in real word it is "0.5000001"
So, doing integer math (like calculating bitmaps address Hi and Lo bytes) do not use CInt(). Use old school INT(). Until you get to negative numbers... see the fix() function.
If there is no need to convert type, use floor().
I think all this chaos of number conversion is for some sort of compatibility with some ancient software.
The difference between those two functions is that they do totally different things:
CInt converts to an Integer type
Math.Round rounds the value to the nearest Integer
Math.Round in this instance will get you 2.0, as specified by the MSDN documentation. You are also using the function incorrectly, see the MSDN link above.
Both will raise an Exception if conversion fails, you can use Try..Catch for this.
Side note: You're new to VB.NET, but you might want to try switching to C#. I find that it is a hybrid of VB.NET & C++ and it will be far easier for you to work with than VB.NET.

Division in VB.NET only shows the whole number

I'm making a simple calculator code, and when I do the division I want it to show not just the whole number but the decimal number.
This is my division code:
get1.Text = Int(mygive.Text \ rate.Text)
I've also tried:
get1.Text = Int(mygive.Text / rate.Text)
I want it to show numbers like this: 2060.0891
Thanks in advance!
You need to convert the numbers to Double prior to doing the division. CDbl is an appropriate Type Conversion Function for this:
Dim answer = CDbl(mygive.Text) / CDbl(rate.Text)
get1.Text = answer.ToString()
Your Int is converting everything to whole numbers. Try converting to double.
It's showing only the whole part of the number because you're declaring an Integer, which is insufficient, as it can only store whole numbers.
You need to use the Double data type.
Also, you're attempting to apply arithmetic operations on Strings, which is invalid.
You need to convert those strings to be of the type Double before you can do / on them.
Use converting to Double/Decimal
with VB help function:
get1.Text = CDec(mygive.Text / rate.Text).ToString()
with .NET function
get1.Text = Convert.ToDecimal(mygive.Text / rate.Text).ToString()

Double or integer? What to use with BIG or SMALL data types?

I have a value of: "2.54334881002458E-37" and i keep getting "overflow" exception when i'm using a double.
what should i use to make this work?
Thank you
code snippet:
Dim curries, act, cat As Double
For Each dataRow As DataRow In dt.Rows
curries = dataRow("Activity")
getting the error when i try to assign Activity to curries.
but "activity" is a string in the database....
Double is already 64 bits worth of floating point number.
Can you post code where you are getting this overflow?
Decimal might be worth a shot, but you have to post code so that we can understand the issues you are encountering.
Based on your edit in your post, why are you storing numbers as strings in your database? That is a definite no no...unless you are not doing any sort of arithmaetic operation only then can you store them as varchar / string.
Give us a sample of what the data looks like...I think your issue stems from not converting the string to a decimal, if activity is a string convert it using DirectCast or CType (cast the value):
curries = CType(datarow("Activity"), Double)
Change your unit of measure, so that you're not working in 10^-37 of whatever it is you're dealing with. This problem just screams "I'm not solving this in the appropriate domain."
According to MSDN, Double should have no problem at all with your number:
Range:
-1.79769313486231570E+308 through -4.94065645841246544E-324 † for negative values;
4.94065645841246544E-324 through 1.79769313486231570E+308 † for positive values
and if the need to have large numbers and precise I suggest the use of decimal numbers, more information here.
http://msdn.microsoft.com/en-us/library/system.decimal.aspx
Bye

What is taking IsNumeric() so long in .NET?

Was doing some benchmarking with IsNumeric today and compared it to the following function:
Private Function IsNumeric(ByVal str As String) As Boolean
If String.IsNullOrEmpty(Str) Then Return False
Dim c As Char
For i As Integer = 0 To Str.Length - 1
c = Str(i)
If Not Char.IsNumber(c) Then Return False
Next
Return True
End Function
I was pretty surprised with the result.
With a numeric value this one was about 8-10 times faster then regular IsNumeric(), and with an empty or non-numeric value it was 1000-1500 times faster.
What is taking IsNumeric so long? Is there something else going on under the hood that I should consider before replacing it with the function above?
I use IsNumeric in about 50 different places all over my site, mostly for validation of forms and query strings.
Where is your check for locale-specific delimiters and decimal places? Negation? Exponential notation?
You see, your function is only a tiny subset of what numeric strings can be.
1,000,000.00
1,5E59
-123456789
You're missing all of these.
A single char can only contains 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
but a full string may contains any of the following:
1
1234
12.34
-1234
-12.34
0001
so IsNumeric ought to be somewhat slower.
There're also cultural and internationalization issues. i.e. If you are processing a Unicode string, does IsNumeric also process numbers from all other languages as well?
Generally speaking, I would avoid duplicating or replacing language features like this, especially in a language like VB, which is bound to be implementing many use cases for you. At the very least, I would name your method something distinct so that it is not confusing to other developers.
The speed difference is bound to be the fact your code is doing far less work than the VB language feature is.
I would use Integer.TryParse() or Double.TryParse() depending on the data type you are using, since both of these account for regional differences.