tags:

views:

486

answers:

3

I have a expression : Select (2345789 * 39.456)/100

The output is 925554.5078400.

I would like to display 925554.50 i.e. 2 values after decimal.

If I use round ( Select round((2345789 * 39.456)/100 ,1) ), the outout is

925554.5000000. But I want exactly 2 digits after the decimal.

How to do that?

A: 

Try (not tested):

SELECT CAST(number as decimal(18,2))
Philippe Leybaert
If I give SELECT CAST(((2345789 * 39.456)/100) as decimal(18,2)), it is wroking fine but it is rounding off too.I am getting 925554.51 instead of 925554.50
priyanka.sarkar
+3  A: 

Stating the obvious, but 925554.5000000 and 925554.50 are the same number.

If you want to display that number with two decimal places then that is an issue for your UI code, not your database.

Having said that, if you must do this in the database itself then try something like:

-- 1 decimal place
SELECT CAST(ROUND(@yourNumber, 1, 1) AS DECIMAL(18, 1))

-- 2 decimal places
SELECT CAST(ROUND(@yourNumber, 2, 1) AS DECIMAL(18, 2))

-- 3 decimal places
SELECT CAST(ROUND(@yourNumber, 3, 1) AS DECIMAL(18, 3))

-- 4 decimal places
SELECT CAST(ROUND(@yourNumber, 4, 1) AS DECIMAL(18, 4))
LukeH
No ,it should not get round off. If I give SELECT CAST(((2345789 * 39.456)/100) as decimal(18,3)), I would expect to get the output as 925554.507 instead of 925554.508.And if I use SELECT CAST(ROUND((2345789 * 39.456) / 100, 1) AS DECIMAL(18, 2)), the output is 925554.500.And also, may be the example that I gave, here it is coming as .50, but if it comes as say1.23498567, then I need 1.23 as opposed to 1.20.Hope you understand.
priyanka.sarkar
@pewned_123: I edited just before your comment appeared. Should be fixed now, I think.
LukeH
Even the same problem is with FLOOR. Though you changed from ROUND to FLOOR. Value should not be rounded off.I want 925554.500 not 925554.508
priyanka.sarkar
@pewned_123: I'm not sure what you mean. Can you give a list of expected inputs and outputs?
LukeH
Sure, for example say the input is 925554.5078400.If I want to display only 2 decimal values, it will be 925554.5.If I want to display only 2 decimal values, it will be 925554.50.If I want to display only 3 decimal values, it will be 925554.507.If I want to display only 4 decimal values, it will be 925554.5078.As you can see, itis not rounded off anywhere
priyanka.sarkar
Sorry, the statement will be If I want to display only 1 decimal values, it will be 925554.5.Instead I typed If I want to display only 2 decimal values, it will be 925554.5
priyanka.sarkar
Great work. thanks a lot.
priyanka.sarkar
A: 

If the rounding is done correctly the answer should be 925554.51.

e.g.

SELECT CAST(((2345789 * 39.456)/100) AS DECIMAL(18,2))
kevchadders
Agreed, although it seems like the OP wants it always rounded down. They should clarify.
LukeH
Hi man, I dont want the value to be rounded off. All the 3 solutions are correct, but it is rounding off.I need, if the value is 1.20456, it shold be 1.20(for 2 decimal places), if I want to display 1.204(for 3 decimal places), the value will be as it is.How to truncate the values to 3 decimal places.
priyanka.sarkar
Ok, I read it as rounding the number. My mistake.
kevchadders