Float Formatting

  • Hello all, and thank you for all of your assistance. This site is truly wonderful in assistaing others.

    My question has to do with FLOAT. I have a table that holds Float Integers. I would like to be able to control the amount of numbers to the right of the decimal. I have tried numerous methods/ways that I have read in the BOL and hard back books, but nothing...... I would like the table to store the Float Int and do the calculations using the Float Int. However, when i write a SP I am not sure how to make the number show only say three or maybe four places to the right of the decimal. Example: the table hold this number: 34.5455897458954, I would like to be able to have that number show in the results of a stored procedure which is shown on a webpage to be only this: 34.5456 Or even when a calculation is done after pulling this number from a ##TempTableName.ColumnName, or when using the TABLE Variable. No matter the means of retreiving/showing the data I would like to have control over how many numbers on the right hand side of the decimal are being displayed.

    Thank You in advance for any assistance that you can offer. Hope that everyone is having a nice day.

    Andrew 🙂


    How long a minute is....
    Depends on what side of the bathroom door you are on.

  • Take a look at CAST function. Cast will allow you to redefine a variable on the fly to another or scale it. Also for your case instead of sticking with Float I suggest would use Decimal or Numeric so you can define the exact scale (or number of digits to the right of the decimal).

    Try

    CAST((Float1/Float2) AS Numeric(10,4))

    For that I would be taking a Float1 divided by Float2 and the result is casted to a Numeric datatype with a total of 10 digits, of which a mximum of 4 will be to the right. I do suggest use more than enough digit space to may sure your scale is as long as you want. See CAST and Data Types in SQL BOL.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • Have you considered using function Round()?

    From BOL:

    ROUND

    Returns a numeric expression, rounded to the specified length or precision.

    Syntax

    ROUND ( numeric_expression , length [ , function ] )

    Arguments

    numeric_expression

    Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.

    length

    Is the precision to which numeric_expression is to be rounded. length must be tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal places specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.

    function

    Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.

    Return Types

    Returns the same type as numeric_expression.

  • Thank You everyone, I will try your suggestions and let you know. I would have choosen the decimal, but the calculations need to be very precise, but showing the results do not have to be.

    Thanks again for your assistance

    Have a nice day 🙂

    How long a minute is....

    Depends on what side of the bathroom door you are on.


    How long a minute is....
    Depends on what side of the bathroom door you are on.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply