如何从给定的十进制年份值计算年份 [英] How to Calculate Year from given decimal Year Values

查看:67
本文介绍了如何从给定的十进制年份值计算年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框

一年又一年一年2和其他文本框总年份

我给输入的第1年文本框值2.3

和第2年文本框值2.8然后

我需要输出总计年份文本框4.11

怎么做这个计算任何人建议我

I Have Three Textbox
One Year 1 another one Year 2 and other textbox total Year
I give the input Year 1 textbox value 2.3
and Year 2 textbox value 2.8 then
I need output is in total Year textbox 4.11
How to do this calculation any one suggest Me

推荐答案

Dim arr1() As String
Dim arr2() As String
Dim intY As Integer
Dim intM As Integer
' Remove extraneous spaces
TextBox1.Text = TextBox1.Text.Trim
' Ensure that there is a period
If Not TextBox1.Text.Contains(".") Then
    TextBox1.Text += "."
End If
' Remove extraneous spaces
TextBox2.Text = TextBox2.Text.Trim
' Ensure that there is a period
If Not TextBox2.Text.Contains(".") Then
    TextBox2.Text += "."
End If
' Split into elements of an array (Index 0=Years, Index 1=Months)
arr1 = TextBox1.Text.Split(CChar("."))
arr2 = TextBox2.Text.Split(CChar("."))
' Convert to Integers and sum the values
Try
    intY = CInt(arr1(0)) + CInt(arr2(0))
    intM = CInt(arr1(1)) + CInt(arr2(1))
Catch ex As Exception
    ' Integer conversion failed for one of the values
    MessageBox.Show("One of the inputs is not a numeric", "Error", _
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Finally
    ' If the months value is greater or equal to 12
    ' Adjust the months value and the year value
    If intM > 11 Then
        intY += intM \ 12
        intM -= (intM \ 12) * 12
    End If
    ' Assign the new computed value to the TextBox
    TextBox3.Text = Format(intY, "###") & "." & Format(intM, "##")
End Try


使用 double.TryParse [ ^ ]将textbox.Text值转换为数值。

一起添加,然后使用ToString设置新文本框Text属性。
Use double.TryParse[^] to convert teh textbox.Text value to a numeric value.
Add then together, and set the new text box Text property using ToString.


如果我正确理解您的要求

没有任何验证或声明:



Pos = InStr(Textbox1.Text,。)

y1 = Val(左(Textbox1.Text,Pos - 1) ))

m1 = Val(mid(Textbox1.Text,Pos + 1))

Pos = InStr(Textbox2.Text,。)

y2 = Val(左(Textbox2.Text,Pos - 1))

m2 = Val(mid(Textbox2.Text,Pos + 1))



Textbox3.Text =(y1 + y2)&。 &(m1 + m2)



如果m1 + m2超过12,会发生什么?如果你希望这变成几年/几个月,你可以使用整数决策来获得div和mod并将div添加到y1和y2。
If I understand your requirement correctly
Without any validation or declarations:

Pos = InStr(Textbox1.Text,".")
y1 = Val(left(Textbox1.Text, Pos - 1))
m1 = Val(mid(Textbox1.Text, Pos + 1))
Pos = InStr(Textbox2.Text,".")
y2 = Val(left(Textbox2.Text, Pos - 1))
m2 = Val(mid(Textbox2.Text, Pos + 1))

Textbox3.Text = (y1 + y2) & "." & (m1 + m2)

What happens if m1 + m2 is more than 12? If you want this turned into years/months, you could use integer dicision to get the div and mod and add the div to y1 and y2.


这篇关于如何从给定的十进制年份值计算年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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