- How To Convert Answer Into Two Decimal Point - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How to convert answer into two decimal point Ask Question Asked 11 years, 7 months ago Modified 5 years, 9 months ago Viewed 169k times 10

This is my code and I want the output which is txtA.Text and txtB.Text to be in two decimal places.

Public Class Form1 Private Sub btncalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalc.Click txtA.Text = Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text) txtB.Text = Val(txtA.Text) * 1000 / Val(txtG.Text) End Sub End Class Share Improve this question Follow edited Nov 22, 2016 at 12:33 Bugs's user avatar Bugs 4,4899 gold badges33 silver badges41 bronze badges asked May 16, 2013 at 7:57 Husna5207's user avatar Husna5207Husna5207 4593 gold badges10 silver badges28 bronze badges Add a comment |

6 Answers 6

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 15

For formatting options, see this

Dim v1 as Double = Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text) txtA.text = v1.ToString("N2"); Share Improve this answer Follow edited May 17, 2013 at 19:06 Victor Zakharov's user avatar Victor Zakharov 26.4k18 gold badges93 silver badges155 bronze badges answered May 16, 2013 at 8:54 fnostro's user avatar fnostrofnostro 4,5911 gold badge16 silver badges23 bronze badges Add a comment | 9

If you have a Decimal or similar numeric type, you can use:

Math.Round(myNumber, 2)

EDIT: So, in your case, it would be:

Public Class Form1 Private Sub btncalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalc.Click txtA.Text = Math.Round((Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text)), 2) txtB.Text = Math.Round((Val(txtA.Text) * 1000 / Val(txtG.Text)), 2) End Sub End Class Share Improve this answer Follow edited May 17, 2013 at 14:56 answered May 16, 2013 at 18:34 Douglas Barbin's user avatar Douglas BarbinDouglas Barbin 3,6152 gold badges16 silver badges34 bronze badges 0 Add a comment | 8

Try using the Format function:

Private Sub btncalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncalc.Click txtA.Text = Format(Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text), "0.00") txtB.Text = Format(Val(txtA.Text) * 1000 / Val(txtG.Text), "0.00") End Sub Share Improve this answer Follow edited May 17, 2013 at 19:05 Victor Zakharov's user avatar Victor Zakharov 26.4k18 gold badges93 silver badges155 bronze badges answered May 16, 2013 at 8:53 Andrea's user avatar AndreaAndrea 12.3k17 gold badges68 silver badges74 bronze badges Add a comment | 4

Call me lazy but:

lblTellBMI.Text = "Your BMI is: " & Math.Round(sngBMI, 2)

I.e.: Label lblTellBMI will display Your BMI is: and then append the value from a Single type variable (sngBMI) as 2 decimal points, simply by using the Math.Round method.

The Math.Round method rounds a value to the nearest integer or to the specified number of fractional digits.

Source: https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx

Share Improve this answer Follow answered May 11, 2016 at 20:44 vicsar's user avatar vicsarvicsar 3316 silver badges18 bronze badges Add a comment | 1

Ran into this problem today and I wrote a function for it. In my particular case, I needed to make sure all values were at least 0 (hence the "LT0" name) and were rounded to two decimal places.

Private Function LT0(ByVal Input As Decimal, Optional ByVal Precision As Int16 = 2) As Decimal ' returns 0 for all values less than 0, the decimal rounded to (Precision) decimal places otherwise. If Input < 0 Then Input = 0 if Precision < 0 then Precision = 0 ' just in case someone does something stupid. Return Decimal.Round(Input, Precision) ' this is the line everyone's probably looking for. End Function Share Improve this answer Follow answered Dec 7, 2016 at 2:04 SEFL's user avatar SEFLSEFL 5694 silver badges15 bronze badges Add a comment | 0

If you just want to print a decimal number with 2 digits after decimal point in specific format no matter of locals use something like this

dim d as double = 1.23456789 dim s as string = d.Tostring("0.##", New System.Globalization.CultureInfo("en-US")) Share Improve this answer Follow edited Mar 22, 2019 at 10:22 answered Mar 22, 2019 at 9:46 manuel 's user avatar manuel manuel 1,8401 gold badge16 silver badges16 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • AI agents that help doctors get paid
  • Legal advice from an AI is illegal
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

3 Need help in FORMAT function 2 (vb.net) rounding to 2 decimal places 5 VB.NET How do I reduce int to 1 decimal place? 12 Convert a string to decimal in VB.NET 0 How to include decimal point to string in vb.net? 2 Unable to convert a double/decimal value with 12 decimal places to a string using visual basic 0 how to make an integer into decimal format in vb 0 How to display a decimal point answer in c# 0 2 decimal result in VB 2 (vb.net) rounding to 2 decimal places 0 Decimal place VB.NET simple 0 Convert integer value to fractional value (decimal)

Hot Network Questions

  • If I sacrifice a Forsaken Miner to the card Eaten Alive do I get the miner back?
  • Obstruction theory for specializing perfect complexes?
  • Japanese passport and Philippine passport with different signatures: ok when traveling to another country?
  • Notepad++ find and replace string
  • How do we provide permission to use a figure that we hired a graphic designer to create in SciRep?
  • Which accents *don't* merge FIRE and HIRE? What about RITE and RIDE?
  • Does using multiple batteries in series or parallel affect mAh
  • Why are there no no-attribution licenses other than "public domain"?
  • Unable to ping mDNS hostname
  • Can a CLA allow selling exceptions without allowing relicensing to no longer be FOSS?
  • Quotient with non equivalence relation
  • Why did Turkish Airlines demand my resident permit for UAE during a transfer?
  • Are probabilities in games of chance any more objective than in other contexts?
  • Should I remove extra water that leaked into sauerkraut?
  • Few doubts about "A new elementary proof of the Prime Number Theorem" by Richter
  • What does the Canada's Access to Information Act entail for reference letters?
  • Regarding Isaiah 9:6, which text has the original rendering, LXX or MT, and why does the false rendering differ significantly from the original?
  • Which lubricant for plastic rail guides on sliding doors?
  • Why are languages commonly structured as trees?
  • Why don't protons and neutrons get ejected by the photoelectric effect?
  • Would having a review article published in a reputable journal as sole author look good to grad programs?
  • British TV show about a widowed football journalist
  • Does fringe biology inpsire fringe philosophy?
  • Can the gravitational force of a black hole "lock" a particle in an unstable equilibrium?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-vb

Từ khóa » Visual Basic Double To Decimal