Results 1 to 6 of 6

Thread: Formatting A Number

  1. #1

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    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.

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Formatting A Number

    You could try something like:
    vb.net Code:
    1. 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)

  3. #3

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    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?

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Formatting A Number

    Quote Originally Posted by Yumby View Post
    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.

    Quote Originally Posted by Yumby View Post
    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)

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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
  •  



Click Here to Expand Forum to Full Width