Results 1 to 11 of 11

Thread: 3 decimal places

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    3 decimal places

    I don't know how to start, How to show the value in the textbox in three decimal places... For example I have value 123.123456, I want it show 123.123

    Thank you

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: 3 decimal places

    Look up the 'Format' function

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: 3 decimal places

    Code:
    Dim Nombor1   As Double
    Dim Nombor2   As Double
    Dim Jawapan As Double
    
    Nombor1 = Val(txtNombor1.Text)
    Nombor2 = Val(txtNombor2.Text)
    
    Jawapan = Nombor1 * Nombor2
    txtJawapan.Text = FormatNumber(Jawapan, 3)
    I use formatnumber, What the difference?

    Code:
    txtJawapan.Text = Format(Jawapan, "#.000")

  4. #4
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Smile Re: 3 decimal places

    if you want to see 3 times 0 after decimal .then you write the following way .so what you want to know ?.try the following . you will see yourself.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim x%
    x = Val(Text1.Text)
    MsgBox Format(x, "#.000")
    End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: 3 decimal places

    When I type the value such as 3 in the textbox, it show 3.000. Why? I just want three decimal places for the floating number I typed in the textbox.

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: 3 decimal places

    Quote Originally Posted by matrik02 View Post
    When I type the value such as 3 in the textbox, it show 3.000. Why? I just want three decimal places for the floating number I typed in the textbox.
    Your question was already answered based on what you said.

    Now, tell us what you actually want to see and on what control that you want the result to appear.
    Doctor Ed

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: 3 decimal places

    Do not use DOUBLE if you want any kind of accuracy - it will loose digits!!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Thumbs up Re: 3 decimal places

    yes szlamany is true . just use long variable instead of double .

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: 3 decimal places

    VB6 has the currency datatype - which is just a LONG datatype with an implied decimal point - it allows for 4 digits to the right of the decimal point...

    Although it's been a few years since I've worked in VB6 solely...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: 3 decimal places

    Just for information :
    LONG datatype has not decimal places:

    Holds signed 64-bit (8-byte) integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807 (9.2...E+18).
    Use the Long data type to contain integer numbers that are too large to fit in the Integer data type.
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: 3 decimal places

    It is quite easy to IMPLY a decimal point in a LONG integer - thus getting non-float accuracy. IMPLIED decimal points have been around since the big COBOL days of the 1950's.

    VB6 does it with the CURRENCY datatype

    Currency is a fixed-point data type, as opposed to thefloating-point Single. In other words, it is always accurate to a specificnumber of decimal places, four in this case. While the Single type canrepresent many more decimal places, these are not needed in Currencycalculations and, in fact, can introduce rounding errors. These errors aresmall but can have an effect on overall accuracy.

    The Currency data type is actually an integer typeinternally. In use, it is scaled by a factor of 10,000 to give four digits tothe right of the decimal point. It permits up to 15 digits to the left of thedecimal point, resulting in a range of approximately -922,337,000,000,000 to+922,337,000,000,000. An individual type Currency variable takes up 8 bytes ofmemory, compared to 4 bytes for Single
    Here's a function in JAVASCRIPT I wrote to do it in a browser - which has no DECIMAL or CURRENCY datatypes!

    It all comes down to this simple line of code

    intMoney = parseInt(strMoney.replace(".", ""), 10);


    Code:
    function parseMoney(strMoney) {
        var intMoney = parseInt(0, 10);
        var intDecimal = 0;
        var intLength = 0;
        if (strMoney.length != 0) {
            strMoney = strMoney.replace(/,/g, "").replace("$", "");
            intDecimal = strMoney.indexOf(".");
            intLength = strMoney.length;
            if (intDecimal == -1) {
                strMoney = strMoney + ".00";
            } else {
                if ((intDecimal + 3) != intLength) {
                    if (intLength == (intDecimal + 1)) {
                        strMoney = strMoney + "00";
                    } else if (intLength == (intDecimal + 2)) {
                        strMoney = strMoney + "0";
                    } else {
                        strMoney = strMoney.substring(0, intDecimal + 3);
                    }
                }
            }
            intMoney = parseInt(strMoney.replace(".", ""), 10);
        }
        return intMoney;
    }
    
    function parseCurrency(intMoney) {
        var strMoney = "";
        strMoney = String(intMoney);
        var blnIsNeg = false;
        if (strMoney.substring(0, 1) == "-") {
            blnIsNeg = true;
            strMoney = strMoney.substring(1, strMoney.length);
        }
        var intLength = strMoney.length;
        if (intLength < 3) {
            strMoney = ("000" + strMoney).substring(intLength, intLength + 3); //("00" + strMoney).substring(0, 2);
            intLength = 3;
        }
        strMoney = ((blnIsNeg) ? "-" : "") + addCommas(strMoney.substring(0, intLength - 2)) + "." + strMoney.substring(intLength - 2);
        return strMoney;
    }

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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