| Author |
Topic |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-11-10 : 19:31:25
|
| How do you do financial rounding in sql 2005?(i.e.)let's work with 2.555, and 2.5551Financial rounding should round 2.555 to 2.55, but 2.5551 to 2.56can you do this with the round function, or do I need to write a UDF for this?Thanks |
|
|
nathans
Aged Yak Warrior
938 Posts |
|
|
doco
Yak Posting Veteran
77 Posts |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-11-13 : 01:08:31
|
Doco, That will not work, please see exampledeclare @Val1 decimal(5,4)declare @Val2 decimal(5,4)set @Val1 = 2.555set @Val2 = 2.5551select cast(@Val1 as money),Cast(@val2 as money)select cast(@Val1 as Decimal(10,2)),Cast(@val2 as Decimal(10,2)) Anyone have any solutions |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-13 : 10:37:49
|
[code]DECLARE @Sample TABLE (Original DECIMAL(5, 4), ShouldBe DECIMAL(5, 4))INSERT @SampleSELECT 2.555, 2.55 UNION ALLSELECT 2.5551, 2.56SELECT Original, ShouldBe, CASE WHEN Original - ROUND(Original, 2, 1) > 0.005 THEN ROUND(Original, 2, 1) + 0.01 ELSE ROUND(Original, 2, 1) END AS Peso1, ROUND(ROUND(Original + 0.0004, 3, 0) - 0.001, 2, 0) AS Peso2FROM @Sample[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-11-13 : 17:59:04
|
| Peso,I was hoping there was a built in function for financial rounding, but your solution of ROUND(ROUND(Original + 0.0004, 3, 0) - 0.001, 2, 0)works very well.Thanks! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-13 : 18:07:13
|
Thank you for the feedback. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
|