Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round And Show To 2 Decimal Places? [duplicate]

Tags:

sql

sql-server

I have a small question to ask. How to round a numeric field upto 2 decimal places, and also show it with 2 decimal places only

For example the following would return 255.88000000000

select round(255.87908765444,2)

How to get 255.88 only?

like image 285
Rejwanul Reja Avatar asked Feb 09 '15 11:02

Rejwanul Reja


People also ask

How do I copy two decimal places in Excel?

Click in an empty cell. On the Formulas tab, under Function, click Formula Builder. In number, type the number you are rounding up.

How do you round double to 2 decimal places on Android?

format("%. 2f", d) , your double will be rounded automatically.


3 Answers

All you need is:

CAST(255.87908765444 as decimal(18,2)). 

When you convert data types in which the target data type has fewer decimal places than the source data type, the value is rounded. From microsoft

like image 187
t-clausen.dk Avatar answered Oct 23 '22 12:10

t-clausen.dk


If you need it as a string, this should work:

select format(round(255.87908765444,2), 'N2');
like image 1
meskobalazs Avatar answered Oct 23 '22 13:10

meskobalazs


use string function substring & char index

select SUBSTRING(convert(varchar(20),round(255.87908765444,2)),
                 1,
                 CHARINDEX('.',convert(varchar(20),255.87908765444))+2)
like image 1
Dudi Konfino Avatar answered Oct 23 '22 13:10

Dudi Konfino