如何在VB.NET中将12.96十进制值转换为00:13:36的时间格式 [英] How to convert 12.96 decimal value into time format as 00:13:36 in VB.NET

查看:159
本文介绍了如何在VB.NET中将12.96十进制值转换为00:13:36的时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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

推荐答案

虽然您可能输入小数,但我认为您真正想要做的是构建从MM.SS格式开始的时间跨度。

如果是这种情况,你可以做的是将该十进制视为(字符串)整数数组,然后构建一个 TimeSpan 来自值。

这个示例很简陋,但应该让你走上正确的轨道
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
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


引用:

如何将12.96十进制值转换为时间格式为vb.net中的00:13:36

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.

引用:

如何编写代码以获得00:13.36作为输出

How to write a code to get 00:13.36 as an output



问题是你很乐意将十进制值混合,实际上是非标准化的时间值和时间值,不要指望标准函数正确处理它。您必须创建代码,在将值作为时间值处理之前将其标准化。

1小时半写入小数点1.5小时或时间值1:30。

12.96表示12分钟+ 96/100分钟,即12分57秒和60/100借调。

如果您使用12.96表示12分钟和96秒,则必须将值标准化,你必须找到数学来规范化12.96到13:36。


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.


想想你将如何在纸上做到这一点。查看小数部分(96)可以除以60的次数,即要添加的分钟数。然后取余数除以60(称为模数或模数),即秒数。



96可被60整除一次96 mod 60是36,所以在整数部分加1给出13小时,36是秒部分。然后,您可以使用DateTime的构造函数从这些值中生成日期,并使用ToString按您的方式格式化它。
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.


这篇关于如何在VB.NET中将12.96十进制值转换为00:13:36的时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆