Decimal Places

TwoWing

Well-known member
Joined
Jan 6, 2006
Messages
71
Location
Near Portsmouth, England
Programming Experience
Beginner
Hi There! I am sure there is a way but I cannot think of it at present. I have a project using cash and therefore there are two places of decimal. But if two or more figures are added (Or whatever) and come to an exact amount I do not get a decimal point followed by two zeros. How can I ensure that I get these two noughts? (I am using RichTextBoxes, if that makes a difference?) I hope you (those especially who have a special few days) have a pleasant weekend.
 
when dealing with currency values (money) i store the amount in a Decimal Variable and when i need to show the amount to the user in a label or textbox or whatever i simply display it as a string formatted for currency such as
VB.NET:
Dim decCustAmount As Decimal = 12D
TxtAmountOwed.Text = decCustAmount.ToString("c")
 
Thanks JuggaloBrotha for your answer but I must admit that being new to .net I am lost, but I shall try that out. If I may explain further - I use a code like this:
VB.NET:
rtbAnswer.Text = Str(Val(rtbA.Text) + Val(rtbB.Text))
If the two numbers being added up don't equal a whole number I get something like 12.34. But if they do, I just get the whole number. I would like to see something like 54.00. How can I get the sum to put in the 2 zeros?
O.K! I know the coding is old-fashioned, but that is something more that I have to get gen on.
Thanks.
 
I would use Convert.ToDecimal instead of Val.
Also surround it in a TRY to catch errors. You should look into only allowing numbers and the decimal point to be entered into rtbA and rtbB, to get the error when entered.
The rounding code (commented out) will round to 2 decimal places.

VB.NET:
        Dim Answer As Decimal
        Try
            Answer = Convert.ToDecimal(rtbA.Text) + Convert.ToDecimal(rtbB.Text)
            rtbAnswer.Text = Answer.ToString("c")
[COLOR=SeaGreen]            'If you need to Round the answer
            'Answer = Math.Round(Answer, 2)
            'rtbAnswer.Text = Answer.ToString[/COLOR]
        Catch ex As Exception
            rtbAnswer.Text = "Error"
        End Try
Hope that helps.
 
Hi. Thanks JuggaloBrotha for you answer - it works a treat!! :)
Thanks also to DavidT_macktool for your method. Either way there is some extra finger work! But if the end justifies it!! Thank you.
 
Easily enough

There is an easier way to do this, there is a built in function in vb.net called FormatNumber. You can use it as follows FormatNumber(250,2). Where 250 is the amount and 2 is the number of decimal places after the number. The output of this function would be: 250.00.
You can manually add the currency sign for the amount.

Hope this helps

Regards
 
Here are three different ways to format currency to 2 places, rounding out as needed:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim money As Double = 23456
Dim mstr As String

mstr = money.ToString("c")
MessageBox.Show(mstr)

mstr = String.Format("{0:c}", money)
MessageBox.Show(mstr)

mstr = FormatCurrency(money)
MessageBox.Show(mstr)
End Sub
 
Back
Top