How can I cast a decimal value to float without getting the result in scientific notation?
For example, if my value is 0.000050
as a decimal, when I cast it to float I get 5E-05
I would like to see 0.00005
Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).
If you are planning to convert varchar to float you should know that these two data types are not compatible with each other. In the earlier versions of SQL Server you had to use CASE, ISNUMERIC & CONVERT to convert varchar to float but in SQL Server 2012, you can do it with just one function TRY_CONVERT.
You can cast to many SQL Server types such as binary , char , varchar , date , datetime , time , decimal , bigint , and float .
That “E” means “exponent” which is easier to understand if you aren't a math geek. Truth is, SQL Server lets you use either a D or an E in scientific notation, thus making for multiple ways to confuse you.
This has nothing to do with converting to float. It has to do with converting to text. You need to look at the str()
function:
str( float_expression , total-width , number-of-decimal-places )
where
In your case, something like:
declare @value float = 0.000050
select str(@value,12,6)
should do you.
Edited to note: the str()
function will not display anything in scientific notation. If the problem is that you want to trim trailing zeroes from the decimal value, you can do two things:
Use the format()
function (SQL Server 2012 only):
declare @x decimal(18,6) = 123.010000
select @x as x1 ,
format(@x,'#,##0.######') as x2 , -- all trailing zeroes trimmed
format(@x,'#,##0.000###') as x3 -- min of 3, max of 6 decimal places shown
use replace()
and trim()
. Works for any version of SQL Server.
declare @x decimal(18,6) = 123.010000
select @x as x1 ,
replace( rtrim(replace(convert(varchar(32),@x),'0',' ')) , ' ' , '0' )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With