Click here to Skip to main content
15,888,977 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>how to convert 12.96 Decimal value into time format as 00:13:36 in vb.net


What I have tried:

how to convert 12.96 Decimal value into time format as 00:13:36 in vb.net
Posted
Updated 21-May-19 6:53am

While you may be entering a decimal, I think what you really want to do is build a timespan from the MM.SS format.
If this is the case, what you could do is to treat that decimal as a (string) Array of integers, and then build a TimeSpan from the values.
This sample is rudimentary, but should get you on the right track
VB
Sub Main()
    Console.WriteLine("Please enter a decimal number")
    Dim entry = Console.ReadLine().Trim()

    Dim DecimalCheck As Decimal = -1
    If (Decimal.TryParse(entry, DecimalCheck)) Then
        Dim TimeValues As String() = entry.Split(".")
        Dim TimeElements As Integer = TimeValues.GetUpperBound(0) + 1

        Dim OutputMinutes As Integer = 0
        Dim OutputSeconds As Integer = 0

        If (TimeElements > 0) Then
            Integer.TryParse(TimeValues(0), OutputMinutes)
            If (TimeElements > 1) Then
                Integer.TryParse(TimeValues(1), OutputSeconds)
            End If
        End If
        Dim OutputTime As TimeSpan = New TimeSpan(0, OutputMinutes, OutputSeconds)
        Console.WriteLine(String.Format("This calculates to: {0}", OutputTime))
    Else
        Console.WriteLine("Invalid decimal entered.")
    End If
    Console.WriteLine("Press ENTER to try again... or type EXIT and then press ENTER to exit")
    Dim Command = Console.ReadLine()
    If (Command.ToUpper().Contains("X")) Then
        Main()
    End If
End Sub
 
Share this answer
 
Quote:
how to convert 12.96 Decimal value into time format as 00:13:36 in vb.net

Probably the same as with any other language. This a mathematical problem.
Quote:
How to write a code to get 00:13.36 as an output

The problem is that you are happily mixing decimal values that are in fact unnormalized time values with time values, do not expect standard functions to deal correctly with that. You have to create code that will normalize the value before handling them as time values.
1 hour and half is written 1.5 hours in decimal or 1:30 in time value.
12.96 means 12 minutes + 96/100 minutes which is 12 minutes 57 secondes and 60/100 of a seconde.
If you use 12.96 to express 12 minutes and 96 secondes, you have to normalize the value, you have to find the mathematics to normalize 12.96 to 13:36.
 
Share this answer
 
Comments
Member 13893129 21-May-19 14:04pm    
You are right? Please explain with function codes.
[no name] 25-May-19 8:43am    
+5
Patrice T 25-May-19 9:23am    
Thank you
Think about how you would do it on paper first. See how many times the decimal part (96) can be divided by 60, that is how many minutes to add. Then take the remainder after it has been divided by 60 (known as the modulus or mod) and that is the number of seconds.

96 is divisible by 60 once and 96 mod 60 is 36, so add 1 to the whole number part giving 13 hours and 36 is the seconds part. You can then use the constructor of DateTime to make a date from those values and use ToString to format it how you please.
 
Share this answer
 
This is more a mathematical question, but I have doubts about your maths.
You take the decimal part (96) and wants to take 60 of it to make the 13th minute, and then add the remaining 36 as seconds?
12.96 minutes are 12 minutes plus 96% of a minute.

It may be better to take the decimal part (0.96) and multiply it by 60, to get the number of seconds (57.6).

And .NET Framework has an even better way for that: the TimeSpan.FromMinutes(Double) Method[^]. You can find examples in VB.NET on that page.
 
Share this answer
 
Comments
Member 13893129 21-May-19 8:58am    
Dim tt As TimeSpan = TimeSpan.FromMinutes(Decimal.ToDouble(12.96))
output : tt = 00:12:57:6000000

How to write a code to get 00:13.36 as an output
phil.o 21-May-19 9:01am    
Let me ask a question:
So 12.96 is 00:13:36.
What is 13.00?
Member 13893129 21-May-19 9:24am    
13.00 is min
phil.o 21-May-19 9:35am    
So, 12.96 (00:13:36) is greater than 13.00 (00:13:00) once turned into a TimeSpan?
Can you see the flaw?
Member 13893129 21-May-19 10:35am    
If we convert 12.96 decimal value into time form then I must be 13.36.
because 96-60 = 36 and remaining 1 add into 12 that 13. so obviously it must 13.36

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900