locked
New to VB and need decimal help RRS feed

  • Question

  • User541471401 posted

    I have this code here ( sorry if its done wrong Ive just decided to learn to code 3 days ago) and am getting the right value but i need to get the decimal point down to the tenths spot. How would I go about that ? (feel free to correct anything else that you may see wrong as it helps me learn)


    If TxtPly1AB.Text = "" Then Exit Sub
    If TxtBaseBall.Text = "" Then Exit Sub
    If TxtPly1HitBy.Text = "" Then Exit Sub
    If TxtPly1Hits.Text = "" Then Exit Sub

    TxtPly1OB.Text = ((Val(TxtPly1Hits.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text)) / (Val(TxtPly1AB.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text))).ToString

    Thursday, May 21, 2015 4:55 PM

Answers

  • User281315223 posted

    If I understand you correctly, you just need to take a value and then round it to the nearest tens place right?

    ' Handle null value checking for all of your textboxes '
    If StringIsNullOrEmpty(TxtPly1AB.Text) OrElse StringIsNullOrEmpty(TxtBaseBall.Text) OrElse StringIsNullOrEmpty(TxtPly1HitBy.Text) OrElse StringIsNullOrEmpty(TxtPly1Hits.Text) Then Exit Sub
    
    ' Calculate your value '
    Dim temp = ((Val(TxtPly1Hits.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text)) / (Val(TxtPly1AB.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text)))
    
    ' Now that you have your value, format it to a single decimal place (using the Math.Round() method'
    TxtPly1OB.Text = Math.Round(temp,1).ToString()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2015 5:18 PM

All replies

  • User281315223 posted

    If I understand you correctly, you just need to take a value and then round it to the nearest tens place right?

    ' Handle null value checking for all of your textboxes '
    If StringIsNullOrEmpty(TxtPly1AB.Text) OrElse StringIsNullOrEmpty(TxtBaseBall.Text) OrElse StringIsNullOrEmpty(TxtPly1HitBy.Text) OrElse StringIsNullOrEmpty(TxtPly1Hits.Text) Then Exit Sub
    
    ' Calculate your value '
    Dim temp = ((Val(TxtPly1Hits.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text)) / (Val(TxtPly1AB.Text) + Val(TxtBaseBall.Text) + Val(TxtPly1HitBy.Text)))
    
    ' Now that you have your value, format it to a single decimal place (using the Math.Round() method'
    TxtPly1OB.Text = Math.Round(temp,1).ToString()

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2015 5:18 PM
  • User541471401 posted

    yes that is all i needed to do . Thanks a TON!

    Thursday, May 21, 2015 5:25 PM