|
-
May 16th, 2008, 07:50 AM
#1
Thread Starter
Addicted Member
Rounding down a decimal value [vs 2005]
Hi,
Does anyone know how to round down a decimal value?
example:
decValue1 = 1.22
I would like to have this rounded to 1
THanks,
-
May 16th, 2008, 07:58 AM
#2
Addicted Member
Re: Rounding down a decimal value [vs 2005]
Math.Ceiling
-
May 16th, 2008, 08:00 AM
#3
Addicted Member
Re: Rounding down a decimal value [vs 2005]
Sorry thats not it... this returns 2
-
May 16th, 2008, 08:02 AM
#4
Fanatic Member
Re: Rounding down a decimal value [vs 2005]
-
May 16th, 2008, 08:03 AM
#5
Addicted Member
Re: Rounding down a decimal value [vs 2005]
Dim a As Double = 1.22
MsgBox(Math.Round(a))
this is it!, good luck, thanks for the status points! 
math.round
Don't forget to mark this post as resolveld!
-
May 16th, 2008, 08:03 AM
#6
Re: Rounding down a decimal value [vs 2005]
-
May 16th, 2008, 08:04 AM
#7
Re: Rounding down a decimal value [vs 2005]
 Originally Posted by Lectere
Dim a As Double = 1.22
MsgBox(Math.Round(a))
this is it!, good luck, thanks you for the status points! 
i thought it was decimal, not double.
-
May 16th, 2008, 08:08 AM
#8
Addicted Member
Re: Rounding down a decimal value [vs 2005]
 Originally Posted by dbasnett
i thought it was decimal, not double.
same result...
-
May 16th, 2008, 08:08 AM
#9
Re: Rounding down a decimal value [vs 2005]
Code:
Dim decNum As Decimal = 1.6D, resDecNum As Decimal
resDecNum = Math.Ceiling(decNum)'2
resDecNum = Math.Floor(decNum)'1
resDecNum = Math.Round(decNum)'2
decNum = 1.4D
resDecNum = Math.Ceiling(decNum)'2
resDecNum = Math.Floor(decNum)'1
resDecNum = Math.Round(decNum)'1
decNum = 1.5D
resDecNum = Math.Ceiling(decNum)'2
resDecNum = Math.Floor(decNum)'1
resDecNum = Math.Round(decNum)'2
Last edited by dbasnett; May 16th, 2008 at 08:12 AM.
-
May 16th, 2008, 08:22 AM
#10
Re: Rounding down a decimal value [vs 2005]
 Originally Posted by Lectere
same result... 
I don't know what the OP had in mind, but if they meant Decimal, then that is what I did.
It seems that a lot of people think these are the same, but they are not.
Dim x as Decimal = 1.22D
Dim x as Double = 1.22 'or single
Unless you have a need for Floating Point numbers I would avoid them, especially for (see signature).
-
May 16th, 2008, 08:30 AM
#11
Thread Starter
Addicted Member
Re: Rounding down a decimal value [vs 2005]
Thanks for all your replies!
What I am trying to establish is the following:
I have two integer values ...
lets say: i have the following calculation
newvalue = value1 / value 2
value1 = 24
value2 = 2459
What datatype should newvalue be?
I had the opinion that it should be a decimal.
Further to that ... I would like to round this number down ... as I am using it to select a case using that newvalue index, which is used to assess the progress of my process.
I am just trying out the math.floor function
Thanks
-
May 16th, 2008, 09:04 AM
#12
Thread Starter
Addicted Member
[SOLVED] Re: Rounding down a decimal value [vs 2005]
Math.Floor works just fine for my decimal variable.
Thanks all ...
-
May 16th, 2008, 12:44 PM
#13
Re: Rounding down a decimal value [vs 2005]
The selection of Data Type should meet the requirements. If you are calculating orbits use double, change from a dollar use decimal, counting from 1 to 100 use integer, etc.
Most of the time, you won't see a difference, but at least in one instance on this forum, I saw decimal v. double make a difference.
Another piece of advice:
Option Strict On
should be line 1 of your code.
-
May 16th, 2008, 01:35 PM
#14
Re: Rounding down a decimal value [vs 2005]
 Originally Posted by dbasnett
Another piece of advice:
Option Strict On
should be line 1 of your code.
I totally agree with you on this. Everyone should turn option strict on in VS Options to make it the default for all projects. Only turn it off manually for the class/module that requires the use of late binding.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
May 16th, 2008, 01:53 PM
#15
Re: Rounding down a decimal value [vs 2005]
Ditto. Option Strict On should be a law for posting code to these forums. When strict conversion is followed, it solves SO many issues, it's not even funny.
-
May 17th, 2008, 06:24 AM
#16
Frenzied Member
Re: Rounding down a decimal value [vs 2005]
I know this is legacy code (but its early in the morning and I haven't got my .NET head on) but it its rounding down every time to the nearest integer then why can't you use:
Seems pretty easy?
-
May 17th, 2008, 08:25 AM
#17
Re: Rounding down a decimal value [vs 2005]
Not what I expected.
Code:
Dim decN As Decimal = -1.6D, nuDecN As Decimal
For x As Integer = 1 To 33
Debug.Write("Number " & decN.ToString)
nuDecN = Int(decN)
Debug.Write(" <Int " & nuDecN.ToString)
nuDecN = Math.Floor(decN)
Debug.Write("> <Floor " & nuDecN.ToString)
nuDecN = Math.Ceiling(decN)
Debug.Write("> <Ceil " & nuDecN.ToString)
nuDecN = Math.Round(decN)
Debug.Write("> <Round " & nuDecN.ToString)
'http://msdn.microsoft.com/en-us/library/k7ycc7xh.aspx
If decN < 0 Then
nuDecN = Math.Ceiling(decN)
Else
nuDecN = Math.Floor(decN)
End If
Debug.WriteLine("> <Choice " & nuDecN.ToString & ">")
decN += 0.1D
Next
Stop
Number -1.6 <Int -2> <Floor -2> <Ceil -1> <Round -2> <Choice -1>
Number -1.5 <Int -2> <Floor -2> <Ceil -1> <Round -2> <Choice -1>
Number -1.4 <Int -2> <Floor -2> <Ceil -1> <Round -1> <Choice -1>
Number -1.3 <Int -2> <Floor -2> <Ceil -1> <Round -1> <Choice -1>
Number -1.2 <Int -2> <Floor -2> <Ceil -1> <Round -1> <Choice -1>
Number -1.1 <Int -2> <Floor -2> <Ceil -1> <Round -1> <Choice -1>
Number -1.0 <Int -1> <Floor -1> <Ceil -1> <Round -1> <Choice -1>
Number -0.9 <Int -1> <Floor -1> <Ceil 0> <Round -1> <Choice 0>
Number -0.8 <Int -1> <Floor -1> <Ceil 0> <Round -1> <Choice 0>
Number -0.7 <Int -1> <Floor -1> <Ceil 0> <Round -1> <Choice 0>
Number -0.6 <Int -1> <Floor -1> <Ceil 0> <Round -1> <Choice 0>
Number -0.5 <Int -1> <Floor -1> <Ceil 0> <Round 0> <Choice 0>
Number -0.4 <Int -1> <Floor -1> <Ceil 0> <Round 0> <Choice 0>
Number -0.3 <Int -1> <Floor -1> <Ceil 0> <Round 0> <Choice 0>
Number -0.2 <Int -1> <Floor -1> <Ceil 0> <Round 0> <Choice 0>
Number -0.1 <Int -1> <Floor -1> <Ceil 0> <Round 0> <Choice 0>
Number 0.0 <Int 0> <Floor 0> <Ceil 0> <Round 0> <Choice 0>
Number 0.1 <Int 0> <Floor 0> <Ceil 1> <Round 0> <Choice 0>
Number 0.2 <Int 0> <Floor 0> <Ceil 1> <Round 0> <Choice 0>
Number 0.3 <Int 0> <Floor 0> <Ceil 1> <Round 0> <Choice 0>
Number 0.4 <Int 0> <Floor 0> <Ceil 1> <Round 0> <Choice 0>
Number 0.5 <Int 0> <Floor 0> <Ceil 1> <Round 0> <Choice 0>
Number 0.6 <Int 0> <Floor 0> <Ceil 1> <Round 1> <Choice 0>
Number 0.7 <Int 0> <Floor 0> <Ceil 1> <Round 1> <Choice 0>
Number 0.8 <Int 0> <Floor 0> <Ceil 1> <Round 1> <Choice 0>
Number 0.9 <Int 0> <Floor 0> <Ceil 1> <Round 1> <Choice 0>
Number 1.0 <Int 1> <Floor 1> <Ceil 1> <Round 1> <Choice 1>
Number 1.1 <Int 1> <Floor 1> <Ceil 2> <Round 1> <Choice 1>
Number 1.2 <Int 1> <Floor 1> <Ceil 2> <Round 1> <Choice 1>
Number 1.3 <Int 1> <Floor 1> <Ceil 2> <Round 1> <Choice 1>
Number 1.4 <Int 1> <Floor 1> <Ceil 2> <Round 1> <Choice 1>
Number 1.5 <Int 1> <Floor 1> <Ceil 2> <Round 2> <Choice 1>
Number 1.6 <Int 1> <Floor 1> <Ceil 2> <Round 2> <Choice 1>
-
May 17th, 2008, 10:43 PM
#18
Re: Rounding down a decimal value [vs 2005]
 Originally Posted by Devdude
Thanks for all your replies!
What I am trying to establish is the following:
I have two integer values ...
lets say: i have the following calculation
newvalue = value1 / value 2
value1 = 24
value2 = 2459
What datatype should newvalue be?
I had the opinion that it should be a decimal.
Further to that ... I would like to round this number down ... as I am using it to select a case using that newvalue index, which is used to assess the progress of my process.
I am just trying out the math.floor function
Thanks
See, this is why it is important to provide a clear and full description of the issue in the first place. If you'd posted this at the start then all this could have been avoided. Division of Integers will ALWAYS return a Double. That's how the division operator is defined for the Integer type. What you should be doing is using integer division:
vb.net Code:
'Note the backslash. Dim quotient As Integer = dividend \ divisor
Integer division will always return an Integer containing only the whole part of the result of a division. Note that Integer division differs from Math.Ceiling and Math.Floor in that it rounds toward zero, while Math.Floor rounds toward -ve infinity and Math.Ceiling will round toward +ve infinity. The code above is equivalent to this:
vb.net Code:
Dim result As Double = dividend / divisor Dim quotient As Integer If result >= 0 Then 'Round down toward zero. quotient = CInt(Math.Floor(result)) Else 'Round up toward zero. quotient = CInt(Math.Ceiling(result)) End If
Which is also equivalent to this:
vb.net Code:
Dim quotient As Integer = CInt(Math.Truncate(dividend / divisor))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|