Multiplying Decimals

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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 said:
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!


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

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


Armin
 
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!

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(TimA)
dim dt as DateTime = CDate("00:00").Add(ts)
TextBox7.Text = dt.ToString("hh:MM")
 
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!
 
Brian,
I normally use a variation of Ross's example:

Dim theTimeSpan As TimeSpan = TimeSpan.FromHours(TimA)
Dim theDate As DateTime = DateTime.MinValue.Add(theTimeSpan)
TextBox7.Text = theDate.ToString("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.ToString
method, which will return the results in the format: [-][d.]hh:mm:ss[.ff]

http://msdn.microsoft.com/library/d...tml/frlrfSystemTimeSpanClassToStringTopic.asp


Dim theTimeSpan As TimeSpan = TimeSpan.FromHours(TimA)
TextBox7.text= theTimeSpan.ToString()

TimeSpan.ToString 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.MinValue.Add(theTimeSpan)
TextBox7.Text = theDate.ToString("hh:mm")

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


For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp


Hope this helps
Jay




|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!
 
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?
 
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.Threading

Dim worker As New Thread
...
worker.Sleep(1000)

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(TimA)

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top