Multiplying Decimals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

    Multiplying Decimals

    I am trying to translate a decimal to time. For example, 7.5 to 7:30. Below
    is my code. If I can multiply the .5 by 60, I will get the :30, but I can't
    figure out how to tell the program to multiply only the .5 and not the entire
    number (TimA or 7.5).

    TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")

    Any help would be greatly appreciated. Thanks!
  • Robin Tucker

    #2
    Re: Multiplying Decimals

    I would write something like:

    60 * ( TimA - ABS ( TimA ) )

    eg.

    = 60 * ( 7.5 - ABS ( 7.5 ) )

    = 60 * ( 7.5 - 7 )

    = 60 * ( 0.5 )

    = 30 :))


    Hmmmm, I'm sure there is a function for getting on the fractional part, but
    I can't remember what it is ;)


    "Brian" <Brian@discussi ons.microsoft.c om> wrote in message
    news:FAD67781-D556-47FC-91E7-3340D4FA49B4@mi crosoft.com...[color=blue]
    >I am trying to translate a decimal to time. For example, 7.5 to 7:30.
    >Below
    > is my code. If I can multiply the .5 by 60, I will get the :30, but I
    > can't
    > figure out how to tell the program to multiply only the .5 and not the
    > entire
    > number (TimA or 7.5).
    >
    > TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")
    >
    > Any help would be greatly appreciated. Thanks![/color]


    Comment

    • Armin Zingler

      #3
      Re: Multiplying Decimals

      Brian schrieb:[color=blue]
      > I am trying to translate a decimal to time. For example, 7.5 to 7:30. Below
      > is my code. If I can multiply the .5 by 60, I will get the :30, but I can't
      > figure out how to tell the program to multiply only the .5 and not the entire
      > number (TimA or 7.5).
      >
      > TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")
      >
      > Any help would be greatly appreciated. Thanks![/color]


      Dim d As Decimal = 7.5D
      Dim ts As TimeSpan = TimeSpan.FromHo urs(d)

      TextBox7.Text = String.Format(" {0:00}:{1:00}", ts.Hours, ts.Minutes)


      Armin

      Comment

      • Brian

        #4
        Re: Multiplying Decimals

        Robin,

        This is what I came up with right after I read your post:

        TextBox7.Text = CStr(Int(TimA)) & ":" & Format(((TimA - Int(TimA)) * 60),
        "00")

        It worked :). Thanks for the help!

        "Robin Tucker" wrote:
        [color=blue]
        > I would write something like:
        >
        > 60 * ( TimA - ABS ( TimA ) )
        >
        > eg.
        >
        > = 60 * ( 7.5 - ABS ( 7.5 ) )
        >
        > = 60 * ( 7.5 - 7 )
        >
        > = 60 * ( 0.5 )
        >
        > = 30 :))
        >
        >
        > Hmmmm, I'm sure there is a function for getting on the fractional part, but
        > I can't remember what it is ;)
        >
        >
        > "Brian" <Brian@discussi ons.microsoft.c om> wrote in message
        > news:FAD67781-D556-47FC-91E7-3340D4FA49B4@mi crosoft.com...[color=green]
        > >I am trying to translate a decimal to time. For example, 7.5 to 7:30.
        > >Below
        > > is my code. If I can multiply the .5 by 60, I will get the :30, but I
        > > can't
        > > figure out how to tell the program to multiply only the .5 and not the
        > > entire
        > > number (TimA or 7.5).
        > >
        > > TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")
        > >
        > > Any help would be greatly appreciated. Thanks![/color]
        >
        >
        >[/color]

        Comment

        • Ross Presser

          #5
          Re: Multiplying Decimals

          On Wed, 6 Jul 2005 10:45:05 -0700, Brian wrote:
          [color=blue]
          > I am trying to translate a decimal to time. For example, 7.5 to 7:30. Below
          > is my code. If I can multiply the .5 by 60, I will get the :30, but I can't
          > figure out how to tell the program to multiply only the .5 and not the entire
          > number (TimA or 7.5).
          >
          > TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")
          >
          > Any help would be greatly appreciated. Thanks![/color]

          First, answering the question as you gave it:

          TextBox7.Text = CStr(int(TimA)) & ":" & _
          Format(( (TimA - int(TimA))* 60, "00")

          Here's another way to accomplish what you want that is perhaps better:

          dim ts as TimeSpan = ts.FromHours(Ti mA)
          dim dt as DateTime = CDate("00:00"). Add(ts)
          TextBox7.Text = dt.ToString("hh :MM")

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Multiplying Decimals

            Brian,
            I normally use a variation of Ross's example:

            Dim theTimeSpan As TimeSpan = TimeSpan.FromHo urs(TimA)
            Dim theDate As DateTime = DateTime.MinVal ue.Add(theTimeS pan)
            TextBox7.Text = theDate.ToStrin g("hh:mm")

            NOTE: MM displays months, while mm displays minutes. FromHours is a shared
            function its "best" to called from the Type itself (TimeSpan), rather then a
            variable theTimeSpan or ts.


            The "easiest" way to format a TimeSpan is to use the TimeSpan.ToStri ng
            method, which will return the results in the format: [-][d.]hh:mm:ss[.ff]

            http://msdn.microsoft.com/library/de...tringTopic.asp


            Dim theTimeSpan As TimeSpan = TimeSpan.FromHo urs(TimA)
            TextBox7.text= theTimeSpan.ToS tring()

            TimeSpan.ToStri ng will optionally include any days & fraction of seconds in
            the converted value.


            To get to hh:mm specifically (or other custom date/time formats) I normally
            convert the TimeSpan to a date.


            Dim theDate As DateTime = DateTime.MinVal ue.Add(theTimeS pan)
            TextBox7.Text = theDate.ToStrin g("hh:mm")

            NOTE: You need to make sure the TimeSpan is positive for the
            DateTime.MinVal ue.Add method to work.


            For details on custom datetime formats see:

            http://msdn.microsoft.com/library/de...matstrings.asp

            For information on formatting in .NET in general see:
            http://msdn.microsoft.com/library/de...ttingtypes.asp


            Hope this helps
            Jay




            "Brian" <Brian@discussi ons.microsoft.c om> wrote in message
            news:FAD67781-D556-47FC-91E7-3340D4FA49B4@mi crosoft.com...
            |I am trying to translate a decimal to time. For example, 7.5 to 7:30.
            Below
            | is my code. If I can multiply the .5 by 60, I will get the :30, but I
            can't
            | figure out how to tell the program to multiply only the .5 and not the
            entire
            | number (TimA or 7.5).
            |
            | TextBox7.Text = CStr(Int(TimA)) & ":" & Format((TimA * 60), "00")
            |
            | Any help would be greatly appreciated. Thanks!


            Comment

            • Ross Presser

              #7
              Re: Multiplying Decimals

              On Wed, 6 Jul 2005 21:35:28 -0500, Jay B. Harlow [MVP - Outlook] wrote:
              [color=blue]
              > NOTE: MM displays months, while mm displays minutes.[/color]

              Oops! I knew that; it's just that I usually get bitten by using mm where I
              meant to use MM, and I forgot where mm really belongs.
              [color=blue]
              > FromHours is a shared
              > function its "best" to called from the Type itself (TimeSpan), rather then a
              > variable theTimeSpan or ts.[/color]

              I can understand that it's "canonical" to call shared functions from the
              type, but are there actual advantages to prefer that to calling them from
              the object? Won't it compile the same?

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: Multiplying Decimals

                Ross,
                | I can understand that it's "canonical" to call shared functions from the
                | type, but are there actual advantages to prefer that to calling them from
                | the object? Won't it compile the same?
                Yes it will compile the same.

                However! it can lead to misleading code as its not obvious if you are
                operating on the instance specified or on something else.

                For example, consider the following:

                Imports System.Threadin g

                Dim worker As New Thread
                ...
                worker.Sleep(10 00)

                Will the current thread or the worker thread go to sleep?

                The above code "clearly" states that you want worker to sleep for 1000,
                however Thread.Sleep is a shared method, that operates on the "Current"
                thread so in actuality the current thread sleeps & not the worker thread!

                Likewise: Looking at your code, if I didn't actually know what FromHours
                did, I would wonder why you are calling a method on an uninitalized variable
                & use that to initialize the variable. As your sample:

                dim ts as TimeSpan = ts.FromHours(Ti mA)

                Implies that "ts" is somehow involved in the FromHours method, when it fact
                it is not...

                Fortunately .NET 2.0 (VB 2005, aka Whidbey
                http://lab.msdn.microsoft.com/vs2005/) causes worker.Sleep to be an warning,
                so you have a chance to correct the misleading code.

                Hope this helps
                Jay

                "Ross Presser" <rpresser@NOSPA Mgmail.com.inva lid> wrote in message
                news:1imzom6qkj 222$.18e9wnl5uk so1.dlg@40tude. net...
                | On Wed, 6 Jul 2005 21:35:28 -0500, Jay B. Harlow [MVP - Outlook] wrote:
                |
                | > NOTE: MM displays months, while mm displays minutes.
                |
                | Oops! I knew that; it's just that I usually get bitten by using mm where I
                | meant to use MM, and I forgot where mm really belongs.
                |
                | > FromHours is a shared
                | > function its "best" to called from the Type itself (TimeSpan), rather
                then a
                | > variable theTimeSpan or ts.
                |
                | I can understand that it's "canonical" to call shared functions from the
                | type, but are there actual advantages to prefer that to calling them from
                | the object? Won't it compile the same?


                Comment

                Working...