Vb.net number truncate. How to do this? - vb.net

I wanna round "2.765467..." number to "2.70".
How can i do that?
İ tried Math.round or math.floor but not working:(
Anyone can send a code for vb.net?

It sounds like what you want to do is truncate, not round. Try this:
Dim decTemp = (10 ^ NumberOfPlaces)
Return CDec(Fix(ToTruncate * decTemp) / decTemp)
Where ToTruncate is the number you'd like to truncate, and NumberOfPlaces is the number of decimal places you want to truncate to. In your case:
Dim ToTruncate As Decimal = 2.765467D
Dim NumberofPlaces as Integer = 1
Dim decTemp = (10 ^ NumberOfPlaces)
Return CDec(Fix(ToTruncate * decTemp) / decTemp)

Related

VB.net Round off Decimal number to the nearest integer

I'm new to VB.net coding i would like to know how to round of a decimal number to the nearest integer
Eg. X= (5-2/2) = 1.5
but I need only as 1.
Thank you.
You can use the integer division operator if you just want to discard any remainder:
Dim resultValue As Integer = (5-2) \ 2
Note that this is one of the differences between VB.NET and C#, in C# the normal division operator will always apply integer division, so discard the remainder.
You have other options:
resultValue = CInt(Math.Floor((5-2) / 2))
resultValue = CInt(Math.Round((5-2) / 2, MidpointRounding.ToZero))
Try this:
Math.Round( (5-2)/2, 0)
..and look into the options for the overload with the MidpointRounding param, which you can set to influence rounding when the result ends with .5. Here are some of the available options:
Math.Round((5 - 2) / 2, MidpointRounding.AwayFromZero)
Math.Round((5 - 2) / 2, MidpointRounding.ToEven)
Check the documentation (or trial & error) to see which best suits your needs.
Or if you always want either the integer below or the integer above, then also check out Math.Floor and Math.Ceiling functions.
I think you can use the floor function. For example, double floor(double x);, the floor function returns the largest integer that is smaller than or equal to x.

Math.round: round number by conditions

I am trying to round a number by the next things:
number with unit digits between 5-10 will be rounded to the nearest 10*x:
(for example: 5->10, 6->10, 27->30, 40->40, 56->60, etc).
number with unit digits between 1-4 will be rounded to 0:
(for example: 4->0, 11->10, 12->10, 20->20, etc).
I want to write it bu Math.Round function.
Meantime, I did it without it:
Dim rest As Integer = r Mod 10
' round up
If rest >= 5 Then
r = r + (10 - rest)
Else ' round down
r = r - rest
End If
Any help appreciated!
Very simple to do with Math.Round
Dim roundedDecade as Double, originalNumber as Double
:
roundedDecade = Math.Round(originalNumber / 10, MidpointRounding.AwayFromZero) * 10
If you want to force the use of integers, just use CDbl and CInt to force some conversions.
Dim roundedDecade as Integer, originalNumber as Integer
:
roundedDecade = CInt(Math.Round(CDbl(originalNumber) / 10, MidpointRounding.AwayFromZero) * 10)

Rounding a variable to an integer

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

Rounding to a whole number in VB.NET

I have code like this:
Dim minutes As Integer = (55 / 60)
I want this to return 0 to me, integer 0 (no decimals), but VB.NET rounds this to 1.
How do I accomplish this?
OK for those with the same problem, try dividing with \ (yeah, it's not escape character in VB.NET).
For full decimal division you have to use another operator.
Try:
\
Dim minutes As Integer = Math.Round(55 / 60)
There are overloads that allow rounding to fractional values and specifying which way to round if the value is exactly between two integers.
So round down? Use:
?Math.Truncate(55/60)
0.0

Syntax for rounding up in VB.NET

What is the syntax to round up a decimal leaving two digits after the decimal point?
Example: 2.566666 -> 2.57
If you want regular rounding, you can just use the Math.Round method. If you specifially want to round upwards, you use the Math.Ceiling method:
Dim d As Decimal = 2.566666
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
Here is how I do it:
Private Function RoundUp(value As Double, decimals As Integer) As Double
Return Math.Ceiling(value * (10 ^ decimals)) / (10 ^ decimals)
End Function
Math.Round is what you're looking for. If you're new to rounding in .NET - you should also look up the difference between AwayFromZero and ToEven rounding. The default of ToEven can sometime take people by surprise.
dim result = Math.Round(2.56666666, 2)
You can use System.Math, specifically Math.Round(), like this:
Math.Round(2.566666, 2)
Math.Round(), as suggested by others, is probably what you want. But the text of your question specifically asked how to "roundup"[sic]. If you always need to round up, regarless of actual value (ie: 2.561111 would still go to 2.57), you can do this:
Math.Ceiling(d * 100)/100D
The basic function for rounding up is Math.Ceiling(d), but the asker specifically wanted to round up after the second decimal place. This would be Math.Ceiling(d * 100) / 100. For example, it may multiply 46.5671 by 100 to get 4656.71, then rounds up to get 4657, then divides by 100 to shift the decimal back 2 places to get 46.57.
I used this way:
Math.Round(d + 0.49D, 2)
Math.Ceiling((14.512555) * 100) / 100
Dot net will give you 14.52. So, you can use above syntax to round the number up for 2 decimal numbers.
I do not understand why people are recommending the incorrect code below:
Dim r As Decimal = Math.Ceiling(d * 100D) / 100D
The correct code to round up should look like this:
Dim r As Double = Math.Ceiling(d)
Math.Ceiling works with data type Double (not Decimal).
The * 100D / 100D is incorrect will break your results for larger numbers.
Math.Ceiling documentation is found here: http://msdn.microsoft.com/en-us/library/zx4t0t48.aspx