Results 1 to 9 of 9

Thread: Decimal Places

  1. #1
    TwoWing is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Jan 2006
    Location
    Near Portsmouth, England
    Posts
    71
    Reputation
    93

    Decimal Places

    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.

  2. #2
    JuggaloBrotha's Avatar
    JuggaloBrotha is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Lansing, MI; USA
    Posts
    4,321
    Reputation
    959
    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
    Code:
    Dim decCustAmount As Decimal = 12D
    TxtAmountOwed.Text = decCustAmount.ToString("c")
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.


  3. #3
    TwoWing is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Jan 2006
    Location
    Near Portsmouth, England
    Posts
    71
    Reputation
    93

    Wink Decimal Places

    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:
    Code:
     
    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.

  4. #4
    DavidT_macktool's Avatar
    DavidT_macktool is offline VB.NET Forum Idol
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Oct 2004
    Location
    Indiana
    Posts
    502
    Reputation
    132
    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.

    Code:
            Dim Answer As Decimal
            Try
                Answer = Convert.ToDecimal(rtbA.Text) + Convert.ToDecimal(rtbB.Text)
                rtbAnswer.Text = Answer.ToString("c")
                'If you need to Round the answer
                'Answer = Math.Round(Answer, 2)
                'rtbAnswer.Text = Answer.ToString
            Catch ex As Exception
                rtbAnswer.Text = "Error"
            End Try
    Hope that helps.
    David C. Talcott

  5. #5
    TwoWing is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Jan 2006
    Location
    Near Portsmouth, England
    Posts
    71
    Reputation
    93

    Decimal Places

    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.

  6. #6
    qwazimoto's Avatar
    qwazimoto is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Oct 2006
    Posts
    39
    Reputation
    83

    Cool or this

    textbox1.text = Format(Awnser,"c")

  7. #7
    Vermiculus's Avatar
    Vermiculus is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jul 2008
    Location
    Baltimore
    Posts
    55
    Reputation
    62
    I just got this idea from JohnH, moderator; you could possibly use a NumericUpDown
    The Wormy has Spoken. Listen and Obey.

  8. #8
    abohmeed is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2008
    Posts
    5
    Reputation
    0

    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

  9. #9
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking