'Round And Show To 2 Decimal Places? [duplicate]

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?



Solution 1:[1]

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

Solution 2:[2]

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

select format(round(255.87908765444,2), 'N2');

Solution 3:[3]

use string function substring & char index

select SUBSTRING(convert(varchar(20),round(255.87908765444,2)),
                 1,
                 CHARINDEX('.',convert(varchar(20),255.87908765444))+2)

Solution 4:[4]

select round(convert(decimal(18,2),255.87908765444),2)

Solution 5:[5]

Yes We can use the above solution is..

ROUND(CAST(psd.Price AS DECIMAL(20,4)), 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 t-clausen.dk
Solution 2 meskobalazs
Solution 3 Dudi Konfino
Solution 4 Hitesh
Solution 5 Rejwanul Reja