|
-
Dec 1st, 2012, 04:03 AM
#1
Thread Starter
Lively Member
Formatting A Number
I want to format a number to a time value. If my number is 2.5, this equals 2 and a 1/2 minutes, so the formatted number would be 02:30:00 (this includes milliseconds.)
I thought x = format(x, "mm:ss:ff") might work but it doesn't. I'm currently trying to / can't work out "toString" or the "Timespan" (if that's what I should be using). So I thought I'd put this out there to see if anyone can help me out. Thanks.
-
Dec 1st, 2012, 05:37 AM
#2
Re: Formatting A Number
You could try something like:
vb.net Code:
MessageBox.Show(New TimeSpan(Convert.ToInt64(m * TimeSpan.TicksPerMinute)).ToString("mm\:ss\:fff"))
where m is a floating-point value in the format specified in the OP.
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Dec 1st, 2012, 07:51 AM
#3
Thread Starter
Lively Member
Re: Formatting A Number
Thanks TJ. I tried variations of your code and this....
Code:
MessageBox.Show(New TimeSpan(Convert.ToInt64(2.5 * TimeSpan.TicksPerMinute)).ToString)
This code got me 00:02:30
But this code....
Code:
MessageBox.Show(New TimeSpan(Convert.ToInt64(dursonchr * TimeSpan.TicksPerMinute)).ToString)
This got me 00:00:00.1190475
In my program, dursonchr is 0.001984125. And the way I get it is this....
Code:
Dim dursonchr
dursonchr = dursonmat / 60 / 24
So any ideas about where I'm going wrong?
-
Dec 1st, 2012, 09:03 AM
#4
Re: Formatting A Number
 Originally Posted by Yumby
Thanks TJ. I tried variations of your code and this....
Code:
MessageBox.Show(New TimeSpan(Convert.ToInt64(2.5 * TimeSpan.TicksPerMinute)).ToString)
This code got me 00:02:30
But with my formatting (as per the request in the OP) it returns 02:30:000 as it was supposed to.
 Originally Posted by Yumby
But this code....
Code:
MessageBox.Show(New TimeSpan(Convert.ToInt64(dursonchr * TimeSpan.TicksPerMinute)).ToString)
This got me 00:00:00.1190475
In my program, dursonchr is 0.001984125. And the way I get it is this....
Code:
Dim dursonchr
dursonchr = dursonmat / 60 / 24
So any ideas about where I'm going wrong?
1) You should use Option Explicit - declaring a variable without type is not the most readable (or efficient, intuitive etc.).
2) I have to assume that dursonmat is a floating point variable containing a portion of a day, so small that it can be counted in 2-digit minutes, 2-digit seconds and 3-digit miliseconds (due to the fact that you divide it by 24, getting hours, and then 60getting minutes). If not your first post dosen't make much sense (ie. anything higher will yield a format of xxxx:yy:zzz, which pretty much is unreadable and should have hours added). And if it is indeed as small as assumed, my code in post #2 will do exactly as you requested.
Tom
#EDIT: You are possibly expecting a result of 02:51:428, in which case, you should just use my original code with dursonmat replacing m.
Last edited by ThomasJohnsen; Dec 1st, 2012 at 09:16 AM.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Dec 1st, 2012, 09:59 AM
#5
Re: Formatting A Number
You started out by asking about a particular value, 2.5, which TJ resolved. Then you showed a variable, dursonchr with no explanation of what it is, as a replacement for the value.
Also you showed some math on the value without explaining what it is supposed to do.
dursonchr = dursonmat / 60 / 24
-
Dec 1st, 2012, 10:13 AM
#6
Re: Formatting A Number
dursonchr = dursonmat / 60 / 24
Must admit I don't understand that. Would it not convert minutes into days (which would appear to be the opposite of what's required)?
2.5 minutes = 0.00173 days
0.001984125 days = 2.857 minutes
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|