Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
is Fun can Do this :
VB
20 -> 20

20.00 -> 20

20.5 -> 20.5

20.5000 -> 20.5

20.125 -> 20.125

20.12500 -> 20.125

0.000 -> 0


in sql??
Posted
Updated 6-Oct-22 19:52pm
Comments
phil.o 9-Jan-14 4:55am    
Not clear : the database engine will return the values as raw decimals/reals/etc...
What you want to do concerns the presentation of the data, thus should not be managed from the database system, but rather from the presentation layer.

try this,
SQL
SELECT convert(DOUBLE PRECISION, 20.00)
SELECT convert(DOUBLE PRECISION, 20.50)
SELECT convert(DOUBLE PRECISION, 20.125)
SELECT convert(DOUBLE PRECISION, 20.12500)

Happy Coding!
:)
 
Share this answer
 
Comments
Maciej Los 9-Jan-14 4:57am    
Welcome back, Aarti ;) Happy New Year!
+5!
Aarti Meswania 9-Jan-14 4:59am    
hi, Maciej Los
Happy new year!
thanks for upvoting :)
Try this also,

SQL
select cast(CAST(12.5800 as decimal(18,5)) as float)
select cast(CAST(12.5 as decimal(18,5)) as float)
select cast(CAST(12.58 as decimal(18,5)) as float)
select cast(CAST(0.00 as decimal(18,5)) as float)

and the out put will be
SQL
12.58
12.5
12.58
0


respectively

thanks
animesh
 
Share this answer
 
Realistically, this is a job for your presentation layer. The values from SQL should be correct. Your presentation layer should care about how they are displayed.
 
Share this answer
 
SELECT CAST (SUM(Amount) as decimal(10,2))
 
Share this answer
 
Comments
CHill60 7-Oct-22 5:19am    
Reasons for my downvote: if you take some of the specific examples the OP used your attempt returns the wrong values. E.g. Instead of 20.12500 -> 20.125 your version gives 20.12500 -> 20.13.
Instead of 20.5000 -> 20.5 yours gives 20.5000 -> 20.50

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900