decimal to two integers

P

Patrick Sullivan

I was having hell trying to get values like 6.50 to return two ints, a = 6
and b = 50.
The data is from a program that uses nn.nn for a time format in hours and
minutes. But 6.50 in decimal would be 6 hours and 30 minutes, not 6 hours
and fifty minutes. I don't know if anyone can use this, but here it is.

Private Function DecToInt(ByVal str As String) As String
Dim c As Integer = 0
Dim lCharacters As Char() = str.ToCharArray()
Dim lNumber As System.Text.StringBuilder = New System.Text.StringBuilder
Do While lCharacters(c) >= "0" And lCharacters(c) <= "9"
lNumber.Append(lCharacters(c))
c += 1
Loop
If lNumber.Length > 0 Then
Return lNumber.ToString
End If
Return "0"
End Function

Private Sub FillInput()
txtName.Text = lstNames.SelectedItem
Dim i As Short
Try
Dim sr As StreamReader = New StreamReader("Names.txt")
Dim line As String
Dim strFileItem As String
Do Until strFileItem = txtName.Text
line = sr.ReadLine()
strFileItem = line.Substring(0, (line.IndexOf(",")))
Loop

' "Patrick Sullivan,04 Feb 1951,6.50,A,6,091.09,30.27" sample data

Dim fields() As String
fields = line.Split(",")
sr.Close()
dtpDate.Value = fields(1).ToString
Dim strValue As String = fields(2).ToString
Dim intDecPlace = strValue.IndexOf(".")
nupdHour.Value = DecToInt(strValue)
nupdMinute.Value = strValue.Substring(intDecPlace + 1, 2)
nupdTZ.Value = fields(4).ToString
txtLongitude.Text = fields(5).ToString
txtLatitude.Text = fields(6).ToString
Catch E As Exception
MsgBox(E.Message)
End Try
End Sub
 
P

Patrick Sullivan

That did not work consistently. Sometimes 6.50 got rounded up to 7 afer
splitting. I don't know why.
 
Q

Qwert

You mean (?):

Dim decTest As Decimal = 6.5
Dim decRest As Decimal = 0
Dim intTest As Integer = 0

intTest = Decimal.Floor(decTest)
decRest = (decTest - intTest) * 100

REM intTest is now 6.
REM decRest is now 50.
 
G

Guest

You could try:

Dim InValue As Double = 6.5
Dim FirstPart As Int32
Dim SecondPart As Int32

SecondPart = System.Math.DivRem(Convert.ToInt32(InValue * 100), 100,
FirstPart)

MessageBox.Show(FirstPart & vbTab & SecondPart)
 
C

Claes Bergefall

Since you're talking about time, the following should work aswell:

Dim dt As Date = DateTime.ParseExact(strValue, "H.mm", Nothing)
nupdHour.Value = dt.Hour
nupdMinute.Value = dt.Minute


/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top