Decimals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?a2FyaW0=?=

    Decimals

    Hello All,
    why is this code is not showing the result in this format: 0.00 and
    showing it as only 0

    Private Sub btn1_Click
    Debug.Print(For mat$(Rnd() * 100, "0.00"))

    Dim d As Double = Math.Round(2250 .0, 3)

    txt2.Text = txt1.Text \ d
  • =?Utf-8?B?a2FyaW0=?=

    #2
    Re: Decimals

    well, i thought that the Debug.print line apply to what is under it. and yes,
    i'm talking about txt2. it always show 0, but i'm trying to get it to show
    0.1

    "Patrice" wrote:
    Are you talking about what is shown in txt2 (is the Debug.print line
    relevant ?)
    >
    What is the part of the code that makes you think it should behave this way
    ? (Math.Round ???)
    >
    --
    Patrice
    >
    "karim" <karim@discussi ons.microsoft.c oma crit dans le message de groupe
    de discussion : 13C1A82C-6AD5-4A59-9625-80AE146D5FD3@mi crosoft.com...
    Hello All,
    why is this code is not showing the result in this format: 0.00 and
    showing it as only 0

    Private Sub btn1_Click
    Debug.Print(For mat$(Rnd() * 100, "0.00"))

    Dim d As Double = Math.Round(2250 .0, 3)

    txt2.Text = txt1.Text \ d
    >
    >

    Comment

    • Armin Zingler

      #3
      Re: Decimals

      "karim" <karim@discussi ons.microsoft.c omschrieb
      Hello All,
      why is this code is not showing the result in this format:
      0.00 and showing it as only 0
      >
      Private Sub btn1_Click
      Debug.Print(For mat$(Rnd() * 100, "0.00"))
      >
      Dim d As Double = Math.Round(2250 .0, 3)
      >
      txt2.Text = txt1.Text \ d
      Please enable Option Strict first. You must become aware of the data types
      you are dealing with. Otherwise you will get unexpected results forever.

      Second, you must convert strings to numeric values. You can not calculate
      with strings.

      "\" is an Integer division operator, that means, you can not divide floating
      point values with it. Use "/" for floating point divisions.

      A Double object has a ToString method that can be used easily.

      Why do you want to round 2250 to 3 decimal places? It already has 0
      decimal places.


      Armin

      Comment

      • =?Utf-8?B?a2FyaW0=?=

        #4
        Re: Decimals

        Hello Armin Zingler
        thank you very much. I didn't have option strict on. and
        i don't want to round to the 2250 to 3 decimal places, what i want is to
        divide whatever in txt1 by 2250 and display it in txt2 and #.###



        Comment

        • Patrice

          #5
          Re: Decimals

          No the format$ *function* takes two input values and return a value. Then
          you print this returned formatted value... Period.

          See also Armin post that contains basically my second round of remarks but I
          wanted first to clarify why you thought you should have seen that before
          moving on more advanced topic.
          You may want to read again a programming introduction tutorial before moving
          on.

          --
          Patrice

          "karim" <karim@discussi ons.microsoft.c oma écrit dans le message de groupe
          de discussion : 4552FD2B-93B1-44DB-98E3-641B6876CAA5@mi crosoft.com...
          well, i thought that the Debug.print line apply to what is under it. and
          yes,
          i'm talking about txt2. it always show 0, but i'm trying to get it to show
          0.1
          >
          "Patrice" wrote:
          >
          >Are you talking about what is shown in txt2 (is the Debug.print line
          >relevant ?)
          >>
          >What is the part of the code that makes you think it should behave this
          >way
          >? (Math.Round ???)
          >>
          >--
          >Patrice
          >>
          >"karim" <karim@discussi ons.microsoft.c oma crit dans le message de
          >groupe
          >de discussion : 13C1A82C-6AD5-4A59-9625-80AE146D5FD3@mi crosoft.com...
          Hello All,
          why is this code is not showing the result in this format: 0.00
          and
          showing it as only 0
          >
          Private Sub btn1_Click
          Debug.Print(For mat$(Rnd() * 100, "0.00"))
          >
          Dim d As Double = Math.Round(2250 .0, 3)
          >
          txt2.Text = txt1.Text \ d
          >>
          >>

          Comment

          • =?Utf-8?B?a2FyaW0=?=

            #6
            Re: Decimals

            well, I tried this:

            txt2.Text = CStr(CDbl(txt1. Text) / 2250)

            but it's showing lots of decimals. how can i get it to show only 2 or 3
            decimals?

            and thanks partice for leting me see that i didn't need the Debug.print or
            the Math.Round.

            the code works, i just needed the decimals. and now that i have the
            decimals, they are too much...

            Comment

            • Andrew Morton

              #7
              Re: Decimals

              karim wrote:
              well, I tried this:
              >
              txt2.Text = CStr(CDbl(txt1. Text) / 2250)
              >
              but it's showing lots of decimals. how can i get it to show only 2 or
              3 decimals?
              >
              and thanks partice for leting me see that i didn't need the
              Debug.print or the Math.Round.
              >
              the code works, i just needed the decimals. and now that i have the
              decimals, they are too much...
              As Armin wrote, you can use .ToString:

              txt2.Text = ((CDbl(txt1.Tex t) / 2250)).ToString ("0.000")

              You can find out about the different things .ToString can do by googling for

              vb.net tostring format

              HTH

              Andrew


              Comment

              • Kevin S Gallagher

                #8
                Re: Decimals

                This should give you two places to the right of the decimal

                Dim Result As Double = CDbl(txt1.Text) / 2250
                txt2.Text = Result.ToString ("##0.00;(##0.0 0);Zero")

                "karim" <karim@discussi ons.microsoft.c omwrote in message
                news:EE3D08AB-A8A3-4906-823B-D0F3EE528201@mi crosoft.com...
                well, I tried this:
                >
                txt2.Text = CStr(CDbl(txt1. Text) / 2250)
                >
                but it's showing lots of decimals. how can i get it to show only 2 or 3
                decimals?
                >
                and thanks partice for leting me see that i didn't need the Debug.print
                or
                the Math.Round.
                >
                the code works, i just needed the decimals. and now that i have the
                decimals, they are too much...

                Comment

                • Armin Zingler

                  #9
                  Re: Decimals

                  "karim" <karim@discussi ons.microsoft.c omschrieb
                  well, I tried this:
                  >
                  txt2.Text = CStr(CDbl(txt1. Text) / 2250)
                  >
                  but it's showing lots of decimals. how can i get it to show only 2
                  or 3 decimals?
                  >
                  and thanks partice for leting me see that i didn't need the
                  Debug.print or the Math.Round.
                  >
                  the code works, i just needed the decimals. and now that i have the
                  decimals, they are too much...

                  txt2.Text = (CDbl(txt1.Text ) / 2250).ToString( "0.000")


                  See also:
                  Explore the type system in .NET. Read about types in .NET (value types or reference types), type definition, type members, and type member characteristics.


                  In addition, I suggest to add some checks because the text in txt1.text
                  might not be convertable to a Double. CDbl will fail then. For example:

                  Dim d As Double

                  If Double.TryParse (txt1.Text, d) Then
                  d = d / 2250
                  txt2.Text = d.ToString("0.0 00")
                  Else
                  MsgBox("invalid input", MsgBoxStyle.Inf ormation)
                  End If



                  Armin

                  Comment

                  Working...