473,422 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

decimal to two integers

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

--

Patrick Sullivan, AA-BA, BA-IT
Nov 23 '05 #1
5 1233
I think you just need to split the decimal by "."?

Nov 23 '05 #2
That did not work consistently. Sometimes 6.50 got rounded up to 7 afer
splitting. I don't know why.

--

Patrick Sullivan, AA-BA, BA-IT

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I think you just need to split the decimal by "."?

Nov 23 '05 #3
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.
"Patrick Sullivan" <ps****@eatel.net> schreef in bericht
news:ts********************@eatel.net...
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

--

Patrick Sullivan, AA-BA, BA-IT

Nov 23 '05 #4
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)

Nov 23 '05 #5
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

"Patrick Sullivan" <ps****@eatel.net> wrote in message
news:ts********************@eatel.net...
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

--

Patrick Sullivan, AA-BA, BA-IT

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
7
by: hana1 | last post by:
Hello experts, I used to program in C/C++ and now switched to Java. I am having a difficulty that I need your help with. How can I limit a double variable to hold 2 decimal points only? Say I...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
13
by: Girish Sahani | last post by:
Hi, I want to truncate every number to 2 digits after the decimal point. I tried the following but it doesnt work. >>> a = 2 >>> b = 3 >>> round(a*1.0 / b,2) 0.67000000000000004
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
17
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.