Confused about format

P

postings

Hi

I have a decimal numbers showing up on my datagrid that is displaying
lots of trailing zeros in the listbox.

i.e. 1.000100
or 1.000000

I want to get rid of all the trailing zeros (I don't want to do any
rounding).

I've tried to use:
number.tostring.format("##0.00")
or
number.tostring.format("{##0.00}"), but all I got was the characters
##0.00 displayed instead!

Actually the original code is like this:

row("Rate").ToString.Format("##0.00")

Where "rate" is a decimal (it's actually a percentage).

I'm very confused! Can you help?

Cheers!

Alex
 
K

Ken Tucker [MVP]

Hi,

Use a numberformatinfo. This is how to get a datagrid to display 3
digits after the decimal. Place this in your tablestyle for the grid.

Dim cm As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource),
CurrencyManager)

Dim pd As System.ComponentModel.PropertyDescriptor =
cm.GetItemProperties()("Qty")

Dim ni As New System.Globalization.NumberFormatInfo

ni.NumberDecimalDigits = 3

Dim colQty As New DataGridTextBoxColumn(pd, "f")

With colQty

..MappingName = "Col1"

..HeaderText = "Qty"

..Width = 75

..FormatInfo = ni

End With



Ken

------------------------------
Hi

I have a decimal numbers showing up on my datagrid that is displaying
lots of trailing zeros in the listbox.

i.e. 1.000100
or 1.000000

I want to get rid of all the trailing zeros (I don't want to do any
rounding).

I've tried to use:
number.tostring.format("##0.00")
or
number.tostring.format("{##0.00}"), but all I got was the characters
##0.00 displayed instead!

Actually the original code is like this:

row("Rate").ToString.Format("##0.00")

Where "rate" is a decimal (it's actually a percentage).

I'm very confused! Can you help?

Cheers!

Alex
 
P

postings

Thanks Ken for your reply

I think I was being unclear.

This is what I want is this to happen:

Decimal displayed in datagrid = 1.234000, decimal I actually want
displayed = 1.234
Decimal displayed in datagrid = 1.234500, decimal I actually want
displayed = 1.2345
Decimal displayed in datagrid = 1.234560, decimal I actually want
displayed = 1.23456
Decimal displayed in datagrid = 1.234567, decimal I actually want
displayed = 1.234567

The actual code I am using is:
li.text = row("Name").ToString & " (" & row("Rate").ToString & "%)"

which at the moment is an item in a listbox that is displaying
something like: "Francs (1.234000%)"
I actually want it displayed as "Francs (1.234%)"

I need to get rid of the trailing zeros which are being displayed,
rather than just display or round up/down to 3 digits.

Thanks

Alex
 
P

Phill. W

.. . .
I've tried to use:
number.tostring.format("##0.00")
or
number.tostring.format("{##0.00}"), but all I got was the characters
##0.00 displayed instead!

Shouldn't that be

number.ToString("##0.00")

ToString() returns a String, (hopefully) containing the formatted
number. When you call Format on this /String/ value, the masking
characters you can use are /different/ (I think "@" is about the only
one that still works).

HTH,
Phill W.
 
P

postings

Thanks Phil

That didn't work for me either. I was very surprised I couldn't get rid
of these trailing zeros easily. I ended up writing my own workaround,
inefficient I guess but effective.

--------------------------------------------------------------------------------------------------------

Function notrailingzeros(ByVal strIn As String) As String

Dim i As Integer
For i = Len(strIn) To 1 Step -1
If Mid(strIn, i, 1) <> "0" Then Exit For
Next

If Mid(strIn, i, 1) = "." Then
Return Mid(strIn, 1, i) & "0"
Else
Return Mid(strIn, 1, i)
End If

End Function
 

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