Limit numbers into 2 decimal places-VBForums
Results 1 to 27 of 27

Thread: Limit numbers into 2 decimal places

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Question Limit numbers into 2 decimal places

    hi!

    someone help me how to limit number into 2 decimal places

    sample when i got number of 1000.7585545 is should be 1000.76

    i have this code below but based on the given sample it will result to 1001.00

    Code:
        If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
            End If
    i tried this one
    but i will result to 1001 only
    Code:
        If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.##")
            End If
    Please help me... how could i get the value 1000.76

    thanks in advance

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Limit numbers into 2 decimal places

    The code you posted first should have given the correct display.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,834

    Re: Limit numbers into 2 decimal places

    For go this method

    vb Code:
    1. dblTranspo.ToString("N2")
    Please mark you thread resolved using the Thread Tools as shown



    www.techreceipe.tk


  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    thanks for the responce opus but the first code i've displayed does not have a correct output... this is the result of the first code

    sample when i got number of 1000.7585545 what i want to is 1000.76
    but the result is 1001.00 and that is wrong...

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,834

    Re: Limit numbers into 2 decimal places

    Quote Originally Posted by danasegarane View Post
    For go this method

    vb Code:
    1. dblTranspo.ToString("N2")
    Have you tried the above one ?
    Please mark you thread resolved using the Thread Tools as shown



    www.techreceipe.tk


  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    danasegarane thanks for responce what is the N2?

  7. #7
    Cyberman Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,493

    Re: Limit numbers into 2 decimal places

    it's the equivalent of the format command. It's an ability of the tostring.

    i dont think the problem in your original code is with the rounding. what do you get when you do this?
    Code:
    msgbox(dbltranspo)
    i would bet you are missing the decimal.
    now this is from memory, but i am betting this is the problem:
    Code:
    dblTranspo = txtpassPrice.Text
    just because dbltranspo is a double, doesn't mean converting from a string will give you a double to put in it. try this instead:
    Code:
    dblTranspo = cdbl(txtpassPrice.Text)
    as another possibility, it may be losing the end of the string after the period due to "regional settings". If it expects a comma to be the decimal separator, it will ignore everything past a period.
    Last edited by Lord Orwell; May 26th, 2009 at 04:33 AM. Reason: added a 2nd solution

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    i have tried the two of the code below but it gived me nothing happened, there is no changes happened..
    Code:
     If txtpassPrice..Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice..Text
                MsgBox(dblTranspo)
            End If
    
     If txtpassPrice..Text <> "" Then
            Dim dblTranspo2 As Double
            dblTranspo2 = txtpassPrice..Text
            dblTranspo2 = CDbl(txtpassPrice..Text)
            MsgBox(dblTranspo2)
            End If

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,834

    Re: Limit numbers into 2 decimal places

    Code:
     Double db = 1000;
                //Get the globalization info for US
                System.Globalization.NumberFormatInfo ninfo = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
                //Set the Number of digits
                ninfo.NumberDecimalDigits = 2;
                //Format as number ..
                //with two decimal points
                Response.Write(db.ToString("N",ninfo));
               db=1000.000512;
                //set the digits as 4
                ninfo.NumberDecimalDigits=4;
                Response.Write(db.ToString("N",ninfo));
    Outputs

    Code:
    1,000.00
    1,000.0005
    Where N points to Number Formating
    Please mark you thread resolved using the Thread Tools as shown



    www.techreceipe.tk


  10. #10
    Cyberman Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,493

    Re: Limit numbers into 2 decimal places

    what did you get from the message box? did it show correctly or were you already missing the digits?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    thanks for the result danasegarane but i don't need the below result..

    1,000.00
    1,000.0005

    but i need this result:1000.76 based on the given numbers in post # 1

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    thanks Lord Orwell, i got the same value as what is found in the textbox there is 100&#37; no changes happened..

  13. #13
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,834

    Re: Limit numbers into 2 decimal places

    Quote Originally Posted by edgarbenilde View Post
    thanks for the result danasegarane but i don't need the below result..

    1,000.00
    1,000.0005

    but i need this result:1000.76 based on the given numbers in post # 1
    You need to try something by changing the example given. This worked for me

    Code:
    Double db = 1000;
                //Get the globalization info for US
                System.Globalization.NumberFormatInfo ninfo = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
                //Set the Number of digits
                db = 1000.7585545;
                //set the digits as 2
                ninfo.NumberDecimalDigits = 2;
                Response.Write(db.ToString("N", ninfo));
    Just change the NumberofDecimalDigits
    Please mark you thread resolved using the Thread Tools as shown



    www.techreceipe.tk


  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    danasegarane what is ninfo?

  15. #15
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Limit numbers into 2 decimal places

    My problem is that I do not get the result you are getting.
    I do use "1000.7585545" for input an get "1000.76".
    I can only guess.
    Are you using any code in the TextBox-Change event (like rounding..)?
    Does the TextBox have a limited length?
    What is the value of dblTranspo before you hand it over to the Textbox (use a messagebox to show it)?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    thanks to opus my first code was working enough i just missing a line of code (the bold one)..

    here is my final code, it's working 100&#37;

    Code:
            If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
                Me.txtpassPrice.Text = Me.txtpassPrice.Text
            End If

  17. #17
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Limit numbers into 2 decimal places

    Quote Originally Posted by edgarbenilde View Post
    thanks to opus my first code was working enough i just missing a line of code (the bold one)..

    here is my final code, it's working 100%

    Code:
            If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
                Me.txtpassPrice.Text = Me.txtpassPrice.Text
            End If
    That just doesn't make any sense - how can setting something to itself possibly change it's value?

  18. #18
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Limit numbers into 2 decimal places

    Quote Originally Posted by edgarbenilde View Post
    thanks to opus my first code was working enough i just missing a line of code (the bold one)..

    here is my final code, it's working 100%

    Code:
            If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
                Me.txtpassPrice.Text = Me.txtpassPrice.Text
            End If
    I have no idea why you did THAT change, nor do I have an idea why it is working for you now!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  19. #19
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Limit numbers into 2 decimal places

    I guess the method 1 was working as intended. It is just that he didn't update the label text where he expected to see his change. I guess everyone who tried to help overestimated the OPs troubleshooting abilities, and assumed that he'd have the common sense of using message boxes or break points.
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    it is working actually i just happened that i forgot to update the value inside the textbox after the value is change... you try on this sample 1000.7585545 and the result should be 1000.76, so it's perfect..
    Code:
       If txtpassPrice.Text <> "" Then
                Dim dblTranspo As Double
                dblTranspo = txtpassPrice.Text
                txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
                Me.txtpassPrice.Text = Me.txtpassPrice.Text
            End If

  21. #21
    Cyberman Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,493

    Re: Limit numbers into 2 decimal places

    in that case this line is completely and irrevocably unnecessary.
    Me.txtpassPrice.Text = Me.txtpassPrice.Text

  22. #22
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    3,817

    Re: Limit numbers into 2 decimal places

    How about we grab the data from the user logically first then display the value with only two decimal places?
    This is assuming you're using VS 2005 and up:
    Code:
    If txtpassPrice.Text <> String.Empty Then
        Dim dblTranspo As Double
        If Double.TryParse(txtpassPrice.Text.Trim, dblTranspo) = True Then
            txtpassPrice.Text = dblTranspo.ToString("n2")
        Else
            txtpassPrice.Text = "Not a valid number!"
        End If
    End If
    And if you're using VS 2008 and up you can do this:
    Code:
    If txtpassPrice.Text <> String.Empty Then
        Dim dblTranspo As Double
        txtpassPrice.Text = If(Double.TryParse(txtpassPrice.Text.Trim, dblTranspo) = True, dblTranspo.ToString("n2"), "Not a valid number!")
    End If

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    thanks you very much JuggaloBrotha your code is alos usefull..

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    Lord if i did not made it it will not give the result i have that is my way... will very thankful for who give the right idea..
    Code:
    in that case this line is completely and irrevocably unnecessary.
    Me.txtpassPrice.Text = Me.txtpassPrice.Text

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    to those who give idea thank you all of you will get points..

  26. #26
    Cyberman Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,493

    Re: Limit numbers into 2 decimal places

    the only way that line could possibly be doing anything is if you have an event triggered when the text is updated.

    Assigning a value to something that already has that value doesn't change that value in any shape or form. You really need to upload the entire form's code so we can see what's really going on.
    Last edited by Lord Orwell; May 27th, 2009 at 03:35 AM.

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2007
    Posts
    839

    Re: Limit numbers into 2 decimal places

    if my post is usefull to anybody please also rate my post

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.