Get highest value from Math.Round

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supun24
    New Member
    • Aug 2006
    • 11

    Get highest value from Math.Round

    Hi..
    I have tried following coding to get highest value.
    EX: If I type 4.341111111111, It should get the value 4.35.
    If I type 9.132222, It should be 9.14 not 9.13.

    Code:
    MsgBox(Math.Round(Val(TextBox1.Text), 2))
    
    ' This is retrieving min value. Please help me to solve  this issue..
    All Codings are with Vb 2008

    Thank You,
    Regards,
    Supun Silva
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    "rounding" is always done as 5 and higher round up, 4 and lower round down.
    So the round function you are using is working as it was meant to.

    You will probably have to write your own function for this uncommon need.

    Comment

    • supun24
      New Member
      • Aug 2006
      • 11

      #3
      Re::

      Do you have any idea for this.. Please help me??

      Thank You,
      Supun Silva

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        If I type 4.341111111111, It should get the value 4.35.
        Multiply the float by 100 that will get you 434.1111111111
        Make an int of that number getting you 434 since it will drop the decimal points
        If the int is greater than the float then add 1 to the float getting you 435.1111111
        Make an int of that getting you 435
        Divide that by 100 getting you 4.35

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Math.Ceiling() always rounds up.
          14.9 or 14.1 will always be 15.
          It might work in decimal form as well with an overload
          OR combine with the multiply by 10 or 100 (experimenting is good)

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by Plater
            Math.Ceiling() always rounds up.
            14.9 or 14.1 will always be 15.
            It might work in decimal form as well with an overload
            OR combine with the multiply by 10 or 100 (experimenting is good)
            Way cool. I love learning new things. There are just so many pre-existing functions that it is impossible to know them all.

            Funny that they didn't use a name that was a little more intuitive:
            Round( )
            RoundUp( )
            RoundDown( )

            This would have also caused all three methods to appear next to each other in the namespace and in Intellisearch, steering people toward them even if they didn't know about them.

            Or an override
            Round(float)
            Round(float, Enum.UpDown)

            This is an example of something I would add to my own namespace with the other method calls to make sense to me.

            Code:
            public static int RoundUp(float Value)
            {
               return Math.Ceiling(Value);
            }

            Comment

            • supun24
              New Member
              • Aug 2006
              • 11

              #7
              Re::

              Hi.. Thanks for your reply. Please be kindly send me the RoundUp code for VB 2008. not for C++. Please send me full cording. I couldn't understand above coding..

              Regards,
              Supun Silva

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Originally posted by supun24
                Hi.. Thanks for your reply. Please be kindly send me the RoundUp code for VB 2008. not for C++. Please send me full cording. I couldn't understand above coding..

                Regards,
                Supun Silva
                Plater already told you how to do this:
                Originally posted by Plater
                Math.Ceiling() always rounds up.
                14.9 or 14.1 will always be 15.
                It might work in decimal form as well with an overload
                OR combine with the multiply by 10 or 100 (experimenting is good)
                Where you are currently using Math.Round just replace with Math.Ceiling

                Comment

                • supun24
                  New Member
                  • Aug 2006
                  • 11

                  #9
                  Re::

                  No.. it is receiving without decimal places. I need number with 2 decimal places.
                  Ex:
                  If I have 14.142324242424 2 --- It should be 14.15.

                  Hope you understand.
                  Regards,
                  Supun Silva

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Hmmmm.... Could there be an answer somewhere in all of this if one were to combine several pieces of advice?

                    Multiply by 100
                    Math.Ceiling
                    Divide by 100

                    Comment

                    Working...