Formatting 2 decimal places?

ProcalX

Member
Joined
Sep 28, 2005
Messages
7
Programming Experience
1-3
At the end of a calculation Pweight stores a number, I need to format this number so that it only shows to 2 decimal places.. I have tried this:

VB.NET:
            If cmbSub.SelectedItem = "NAC(N - acetlycsteine)" Then
                While i > 0
                    'Calculate current weight - % annual deterioration
                    Pweight = Pweight - (Pweight * Deterioration / 100)

                    'remove 1 year from each succesful loop
                    i = i - 1
                End While
                txtResult.Text = Pweight
                lblMsg2.Text = Today + "."
                lblMsg3.Text = "This is a calculation of the deterioration of " + Oweight.ToString()
                lblMsg4.Text = "Grams of " + cmbSub.SelectedItem + " substance. The amount remaining of " + cmbSub.SelectedItem + " after " + cmbYear.SelectedItem + " years is " + Pweight.ToString(Format(1.9, "0.00")) + "."
            End If

This actually seems to make it miss calculate, so I've put it back to Pweight.ToString() and left it at that but I get around 12 decimal places.
 
When concatenating strings use the & operator not the + (addition) operator.

VB.NET:
Pweight.ToString("0.00")
 
Personally I would do .ToString("N:2")... the "N" means it's a number and the "2" means 2 decimal places.
 
There's always the old school FormatCurrency function aswell... check out Microsoft.VisualBasic.Strings.FormatCurrency.
 
There's always the old school FormatCurrency function aswell... check out Microsoft.VisualBasic.Strings.FormatCurrency.
The only thing with that is he's calculating a weight (not cost) of a substance and he want's it displayed to 2 decimal places. The FormatCurrency() function will add a $ to the beginning of it, which represents money, not an item/element's weight.

Even if he wanted it displayed as currency, I wouldn't use the vb6 legacy function for that especially since .ToString("c") outputs currency for you.
 
Back
Top