473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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!
Nov 21 '05 #1
7 3752
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" <Br***@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.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!

Nov 21 '05 #2
Brian schrieb:
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
Nov 21 '05 #3
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:
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" <Br***@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.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!


Nov 21 '05 #4
On Wed, 6 Jul 2005 10:45:05 -0700, Brian wrote:
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")
Nov 21 '05 #5
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/de...tringTopic.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/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" <Br***@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.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!
Nov 21 '05 #6
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?
Nov 21 '05 #7
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

"Ross Presser" <rp******@NOSPAMgmail.com.invalid> wrote in message
news:1i******************************@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?
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: k | last post by:
I have aproblem when multiplying 2 float 638.9 * 382.8 should = 244570.92 results giving 244570.922 both numbers are float variables , tried using double to...
20
by: craigslist.jg | last post by:
Hi, I'm building an IE-centric portal for an intranet so I've only had to develop for IE 6+ My version is: 6.00290.2180 Here's what I'm seeing: 0.25 * 100 = 25
6
by: Kuldeep | last post by:
Hi All, I have this piece of code shown below: decimal ft = Convert.ToDecimal(txtft.Text); decimal inch = Convert.ToDecimal(txtin.Text); decimal metre = ((ft * 12) + inch) * 0.0254;
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
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(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.