Home Cast everything for 2 decimal points?
Reply: 2

Cast everything for 2 decimal points?

rbhat
1#
rbhat Published in 2017-12-06 19:17:53Z

I want to divide two integers: 8/15 and return "53.33".

I've tried every single combination, and I've found that this is the only way I can return the desired value:

select cast(100*cast(8/cast(15 as decimal(10,4)) as decimal(18,4)) as decimal(18,2))

Is there a shorter way?

Thanks.

scsimon
2#
scsimon Reply to 2017-12-06 19:30:57Z

You are doing interger division. Use float, and multiply by 100.

select 8/15.00 * 100

Or, for your precision, just do the cast once

select cast(8/15.00 * 100 as decimal (10,2))

If these are integers in a table then multiple by 1.0 and cast once

DEMO

declare @table table (int_one int, int_two int)
insert into @table 
values
(8,15)

select 
    cast(((int_one * 1.0) / int_two) * 100 as decimal(10,2))
from @table
scsimon
3#
scsimon Reply to 2017-12-06 19:25:20Z

try this:

select (cast (8 as money)/cast(15 as money) * 100)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305117 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO