Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Display only 1 decimal place of an Int or Double

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    42

    Resolved [RESOLVED] [2005] Display only 1 decimal place of an Int or Double

    Hi all, this should be an easy one:

    How can you show only one decimal place of a Double, not lose the remaining accuracy just only show (eg in a text box) 1dp.

    Dim number as Double = 1.2345
    textbox1.text = "<here i want to only show 1.2>"

    thanks all

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [2005] Display only 1 decimal place of an Int or Double

    Here's one way....

    VB Code:
    1. Dim dl As Double
    2.         dl = 1.2345
    3.         TextBox1.Text = dl.ToString.Substring(0, dl.ToString.IndexOf(".") + 2)
    If my post helps , please feel free to rate it

  3. #3
    Lively Member
    Join Date
    Jul 2006
    Posts
    84

    Re: [2005] Display only 1 decimal place of an Int or Double

    even easier...
    VB Code:
    1. Textbox1.Text = dl.ToString("#.#")

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] [2005] Display only 1 decimal place of an Int or Double

    Just for further info, you should check out the Round function in the System.Math class.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Display only 1 decimal place of an Int or Double

    Quote Originally Posted by jamesclarke112
    even easier...
    VB Code:
    1. Textbox1.Text = dl.ToString("#.#")
    I would suggest using standard numeric format strings in preference to custom numeric format strings wherever possible. That way you ensure that you use the current systems chosen formats. To display a number with a single decimal place would be:
    VB Code:
    1. myNumber.ToString("n1")
    You can find all the possibilities, both standard and custom, simply by searching MSDN for format strings. You'll find information for numbers and dates/times.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width