ToString Number Format

K

K

Hi all, The below code displays result in 1,234.00 number format. So
if I have amount 2,000.49 in cell one and 2,000.01 in cell two of
datagridview the result should be 4,000.50 but below code displays it
4,001.00. How can I get the the decimals? Also if I want result to
be 4,001 instead of 4,001.00 in other words amount without decimals
then how can I achive it in below code. Please can any friend tell me
that what amendment in below code can give me the result i am looking
for.


Private Sub DataGridViewX2_SelectionChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DataGridViewX2.SelectionChanged
Dim OBC As Integer = 0
Dim currentRow As DataGridViewCell
For Each currentRow In DataGridViewX2.SelectedCells
If currentRow.ColumnIndex = 6 And
DataGridViewX2.SelectedCells.Count > 1 Then
OBC += CInt(currentRow.Value)
ElseIf currentRow.ColumnIndex = 9 And
DataGridViewX2.SelectedCells.Count > 1 Then
OBC += CInt(currentRow.Value)
Else
OBC += 0
End If
Next
LabelItem2.Visible = True
LabelItem3.Visible = True
If Microsoft.VisualBasic.Left(OBC.ToString, 1) = "-" Then
LabelItem3.Text = OBC.ToString("N")
LabelItem3.ForeColor = Color.Red
Else
LabelItem3.Text = OBC.ToString("N")
LabelItem3.ForeColor = Color.Black
End If
End Sub
 
Joined
Jan 26, 2012
Messages
2
Reaction score
0
Use Math.Round to determine how many decimal places - Math.Round(Value, Decimal Places)

e.g. Math.Round(200.47, 0) would return 200 and Math.Round(200.51) would return 201

For converting to string use Format

Dim vString as String = Format(200.47, "###,##0") will return 200, Format(200.1483, "###,##0.00") will return 200.15
 
J

Jason Keats

K said:
Hi all, The below code displays result in 1,234.00 number format. So
if I have amount 2,000.49 in cell one and 2,000.01 in cell two of
datagridview the result should be 4,000.50 but below code displays it
4,001.00. How can I get the the decimals? Also if I want result to
be 4,001 instead of 4,001.00 in other words amount without decimals
then how can I achive it in below code. Please can any friend tell me
that what amendment in below code can give me the result i am looking
for.


Private Sub DataGridViewX2_SelectionChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DataGridViewX2.SelectionChanged
Dim OBC As Integer = 0
Dim currentRow As DataGridViewCell
For Each currentRow In DataGridViewX2.SelectedCells
If currentRow.ColumnIndex = 6 And
DataGridViewX2.SelectedCells.Count> 1 Then
OBC += CInt(currentRow.Value)
ElseIf currentRow.ColumnIndex = 9 And
DataGridViewX2.SelectedCells.Count> 1 Then
OBC += CInt(currentRow.Value)
Else
OBC += 0
End If
Next
LabelItem2.Visible = True
LabelItem3.Visible = True
If Microsoft.VisualBasic.Left(OBC.ToString, 1) = "-" Then
LabelItem3.Text = OBC.ToString("N")
LabelItem3.ForeColor = Color.Red
Else
LabelItem3.Text = OBC.ToString("N")
LabelItem3.ForeColor = Color.Black
End If
End Sub

Try changing "As Integer" to "As Single" and all CInt to CSng.
 

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

Similar Threads


Top