简体   繁体   中英

SQL Convert float to comma varchar with decimals

I have a couple floats that are kinda big. They're around a 100 million.

I would like this number to show like the following 123,456,789.01234

I've found that I can use CONVERT if its a money datatype but this doesn't do the full trick (it leaves off some decimal places).

I have to have commas on the left and five decimal places on the right.

Is there any built in SQL function to help with this? Or do I have to write a custom function?

Thanks

*** Update

  • I forgot to mention that I'm just displaying these as varchars. So there isn't any calculations after this.
  • This is running on an SQL database so MySQL and Oracle won't work.
DECLARE @f FLOAT

SET @f = 123456789.01234

SELECT  LEFT('$' + CONVERT(VARCHAR(20), CAST(@f AS MONEY), 1), LEN(@f) - 2)

this will cut it up to two places of decimal for formatting. You can change LEN(@f) - 2 to modify this setting.

if you are just displaying this as text you can do the following:

oracle :

select to_char(123456789.01234,'999,999,999.99999') from dual; => 123,456,789.01234

MySQL :

select format(123456789.01234,5) => 123,456,789.01234<br>

the MySQL function rounds

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