Limit any number to four digits?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RonHouston
    New Member
    • Oct 2006
    • 10

    Limit any number to four digits?

    Hi,

    I was wondering if someone could help me on this one.
    Is there anyway to format a string/integer etc so that it always outputs a 4 digits number?
    thank you.
    Ron.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by RonHouston
    Hi,

    I was wondering if someone could help me on this one.
    Is there anyway to format a string/integer etc so that it always outputs a 4 digits number?
    thank you.
    Ron.

    This should help:

    MsgBox Format(23, "0000")

    Comment

    • RonHouston
      New Member
      • Oct 2006
      • 10

      #3
      Hi,
      thanks a lot sire.
      May I ask what the 23 is for?

      again, Thank you

      Ron.

      Comment

      • RonHouston
        New Member
        • Oct 2006
        • 10

        #4
        it didn't work.
        here's what I have:
        Code:
        variableOne = algorithm_here
        //for this example sake let's say
        //variableOne = 0.99999999999999 that's 14 times the number 9
        textBox = variableOne
        Instead of textBox showing the whole number I'd like it to show only the first 4

        thank you

        Ron.

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by RonHouston
          it didn't work.
          here's what I have:
          Code:
          variableOne = algorithm_here
          //for this example sake let's say
          //variableOne = 0.99999999999999 that's 14 times the number 9
          textBox = variableOne
          Instead of textBox showing the whole number I'd like it to show only the first 4

          thank you

          Ron.
          Ah that is different. You must distinguish between either side of the decimal point. You can use the Round() function to limit decimal places to 4:

          variableOne = Round(variableO ne, 4)

          But that will only work with the decimal point and, especially with the number you are using (0.999999999999 ), you may keep getting '1'

          Good luck

          Comment

          • RonHouston
            New Member
            • Oct 2006
            • 10

            #6
            Hi, and thank you willakawill,
            that's almost what I want, althouth Round actually rounds the variable variableOne.
            All I want it to do is to troncate... and I was hoping there would be a way to Format a variable to only hold the first 4 digits, without rounding it.

            Actually what if I wanted only the four numbers after the decimal places.

            if variableOne = 0.1234567890
            then I would want it to be 0.1234

            thanks again, and sorry for changing the question.
            Ron.

            Comment

            • RonHouston
              New Member
              • Oct 2006
              • 10

              #7
              variableOne = Format(variable One, "#.####")

              thought this would work but it doesn't really work
              it doesn't add a zero to numbers that have less than 4 digits after the decimal.

              Ron.

              Comment

              • RonHouston
                New Member
                • Oct 2006
                • 10

                #8
                this actually worked:
                variableOne = Format(variable One, "0.0000")
                thanks a lot everyone.

                Ron.

                Comment

                • RonHouston
                  New Member
                  • Oct 2006
                  • 10

                  #9
                  Hi again.

                  Using the above formatting technique won't stop variableOne to display in scientific format. any way to always have zeros instead of E?

                  thanks

                  Ron.

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    What is the variable type of variableOne?

                    Comment

                    • RonHouston
                      New Member
                      • Oct 2006
                      • 10

                      #11
                      Hi,

                      it's a single, and it needs to be one

                      Thanks.
                      Ron.

                      Comment

                      • willakawill
                        Top Contributor
                        • Oct 2006
                        • 1646

                        #12
                        Originally posted by RonHouston
                        Hi,

                        it's a single, and it needs to be one

                        Thanks.
                        Ron.
                        When you assign the result of the Format function back to the variable you will loose the formatting. If you have to assign the result then you should use a text variable

                        Comment

                        • RonHouston
                          New Member
                          • Oct 2006
                          • 10

                          #13
                          Hi,

                          thanks for the help.
                          I'm still a bit stuck though. Sorry to bother you this much.

                          the variable that doesn't format properly is actually variableThree
                          where
                          variableThree = variableOne + variableTwo

                          I tried putting these two lines but it won't work:
                          variableTwo = format(variable Two, "0.0000")
                          variableThree = format(variable Three, "0.0000")
                          but variableThree would still appear as scrientific notation.
                          and variableOne, variableTwo, and variableThree are all Single

                          thank you in advance.

                          Ron.

                          Comment

                          • willakawill
                            Top Contributor
                            • Oct 2006
                            • 1646

                            #14
                            Originally posted by RonHouston
                            Hi,

                            thanks for the help.
                            I'm still a bit stuck though. Sorry to bother you this much.

                            the variable that doesn't format properly is actually variableThree
                            where
                            variableThree = variableOne + variableTwo

                            I tried putting these two lines but it won't work:
                            variableTwo = format(variable Two, "0.0000")
                            variableThree = format(variable Three, "0.0000")
                            but variableThree would still appear as scrientific notation.
                            and variableOne, variableTwo, and variableThree are all Single

                            thank you in advance.

                            Ron.
                            The Format function returns a string. You can't assign it to a single. It won't work.

                            stThisMustBeASt ringVariable = Format(anything you want to format here)
                            or
                            MsgBox Format(anthing)
                            or
                            Me.txtTextBox.T ext = Format(anything )

                            Use the Format function to format display text after you have completed all calculations. Don't try to store the results of formatting back into a number variable.

                            Comment

                            • RonHouston
                              New Member
                              • Oct 2006
                              • 10

                              #15
                              Hi,

                              thanks everyone. I finally got everything to work properly.

                              it's really appreciated.

                              Thanks again.
                              Ron.

                              Comment

                              Working...