简体   繁体   中英

How to convert Float to Varchar in SQL

I am doing this

declare @num float = 7708369000

select  @num as [float], 
    convert(varchar, @num) as [varchar]

it gives me this

float                  varchar
---------------------- ------------------------------
7708369000             7.70837e+009

But I want this

float                  varchar
---------------------- ------------------------------
7708369000             7708369000

Please help.

convert it to decimal first,

CAST(CAST(@num AS DECIMAL(20)) AS VARCHAR(20))

Try using the Str function rather than using convert

DECLARE @num float = 7708369000;    
SELECT Str(@num);

Try one of the following variations (values represent Max Value of BigInt and other Max type values) that meet your needs below. http://sqlfiddle.com/#!6/745c8f/1

CREATE TABLE Table1 
(
   FloatDefault float,  --Default is 24 bits giving 7 digit precision and 4 bytes storage
   Float24 float(24),   --Max for 4 bytes storage, 24 bits giving 7 digit precision and 4 bytes storage
   Float53 float(53)    --Max for 8 bytes storage, 53 bits giving 15 digit precision and 8 bytes storage
);

INSERT INTO Table1 VALUES(-9223372036854775808, -9223372036854775808, -9223372036854775808); --Max Negative Value of a Big Int
INSERT INTO Table1 VALUES(9223372036854775807, 9223372036854775807, 9223372036854775807);  --Max Positive Value of a Big Int
INSERT INTO Table1 VALUES(-2147483648, -2147483648, -2147483648); --Max Negative Value of a Big Int
INSERT INTO Table1 VALUES(2147483647, 2147483647, 2147483647);  --Max Positive Value of a Big Int
INSERT INTO Table1 VALUES(123456789012345678901234567890, 123456789012345678901234567890, 123456789012345678901234567890);
INSERT INTO Table1 VALUES(123456789012345678901234567890.12345678, 123456789012345678901234567890.12345678, 123456789012345678901234567890.12345678);
INSERT INTO Table1 VALUES(1234567890, 1234567890, 1234567890);
INSERT INTO Table1 VALUES(1234567890.0123456789, 1234567890.0123456789, 1234567890.0123456789);
INSERT INTO Table1 VALUES(22.0/7.0, 22.0/7.0, 22.0/7.0); -- Value of Pi
INSERT INTO Table1 VALUES(1, 1, 1);
INSERT INTO Table1 VALUES(2.0, 2.0, 2.0);
INSERT INTO Table1 VALUES(2000.0, 2000.0, 2000.0);

SELECT 
   FloatDefault,
   Float24,
   Float53,   
   CAST(CAST(Float53  AS NUMERIC(38)) AS VARCHAR(100)), -- 38 is the max precision 
   CAST(CAST(Float53  AS NUMERIC(38,5)) AS VARCHAR(100)), 
   STR(Float53),
   STR(Float53, 38),
   STR(Float53, 38,5),
   LTRIM(RTRIM(STR(Float53, 38,5))),
   CONVERT(VARCHAR, Float53),
   CONVERT(VARCHAR(100), Float53),
   CONVERT(NUMERIC(38,5), Float53)
FROM Table1
GO

Use following function:

STR(number, length, decimal)

  • number is the numeric value to convert to a string
  • length is the length of the result string. Default value is 10
  • decimal is the number of decimals places to round off. Default value is 0

Ref: STR (Transact-SQL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM