Results 1 to 18 of 18

Thread: Rounding down a decimal value [vs 2005]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    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,

  2. #2
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: Rounding down a decimal value [vs 2005]

    Math.Ceiling


  3. #3
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: Rounding down a decimal value [vs 2005]

    Sorry thats not it... this returns 2

  4. #4
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: Rounding down a decimal value [vs 2005]

    then math.floor

  5. #5
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    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!

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Rounding down a decimal value [vs 2005]

    math.floor
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Rounding down a decimal value [vs 2005]

    Quote 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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Addicted Member Lectere's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    222

    Re: Rounding down a decimal value [vs 2005]

    Quote Originally Posted by dbasnett
    i thought it was decimal, not double.
    same result...

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Rounding down a decimal value [vs 2005]

    Quote 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).
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Resolved [SOLVED] Re: Rounding down a decimal value [vs 2005]

    Math.Floor works just fine for my decimal variable.


    Thanks all ...


  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Rounding down a decimal value [vs 2005]

    Quote 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 -

  15. #15
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  16. #16
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    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:

    Code:
    var = Int(var)
    Seems pretty easy?

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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>
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Rounding down a decimal value [vs 2005]

    Quote 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:
    1. 'Note the backslash.
    2. 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:
    1. Dim result As Double = dividend / divisor
    2. Dim quotient As Integer
    3.  
    4. If result >= 0 Then
    5.     'Round down toward zero.
    6.     quotient = CInt(Math.Floor(result))
    7. Else
    8.     'Round up toward zero.
    9.     quotient = CInt(Math.Ceiling(result))
    10. End If
    Which is also equivalent to this:
    vb.net Code:
    1. Dim quotient As Integer = CInt(Math.Truncate(dividend / divisor))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width