ROUND

martedì 23 maggio 2006 - 10.31

mg.macmac Profilo | Newbie

Ciao a tutti,
la funzione ROUND(16,365;2) mi da come risultato 16,37, arrotondando il 5 al numero superiore.
Come devo fare per considerare il 5 come numero inferiore ottennedo il risultato 16,36 senza utilizzare il TRUNCATE, che mi falserebbe altre situazioni?
Grazie a tutti

lbenaglia Profilo | Guru

>la funzione ROUND(16,365;2) mi da come risultato 16,37, arrotondando
>il 5 al numero superiore.

Ciao mg.macmac,

che DBMS stai utilizzando?

>Come devo fare per considerare il 5 come numero inferiore ottennedo
>il risultato 16,36 senza utilizzare il TRUNCATE, che mi falserebbe
>altre situazioni?

L'arrotondamento che riporti è corretto. In SQL Server 2005 la funzione ROUND() prevede un terzo parametro che se valorizzato a 0 effettua l'arrotondamento, diversamente effettua il troncamento del numero.

Esempio:

SELECT ROUND(16.365, 2) AS Arrotondamento , ROUND(16.365, 2, 1) AS Troncamento /* Output: Arrotondamento Troncamento --------------- ------------ 16.370 16.360 (1 row(s) affected) */

Per maggiori dettagli:

"ROUND (Transact-SQL)"
http://msdn2.microsoft.com/en-us/library/ms175003.aspx

>Grazie a tutti
Prego.

Ciao!

--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org

mg.macmac Profilo | Newbie

Ciao Lorenzo,
grazie per la risposta ma io devo fare questo :

16,352 = 16,35
16,355 = 16,35
16,356 = 16,36

mentre il round mi da :

16,352 = 16,35
16,355 = 16,36
16,356 = 16,36

e quello indicato, uguale al truncate, mi da:

16,352 = 16,35
16,355 = 16,35
16,356 = 16,35

Sai qualche altro stratagemma ?

Ciao e grazie ancora.

lbenaglia Profilo | Guru

>Sai qualche altro stratagemma ?

E se provassi a sottrarre 1 millesimo?

USE tempdb; GO CREATE TABLE dbo.Numbers( Number decimal(5, 3) NOT NULL ); GO INSERT dbo.Numbers VALUES(16.350); INSERT dbo.Numbers VALUES(16.351); INSERT dbo.Numbers VALUES(16.352); INSERT dbo.Numbers VALUES(16.353); INSERT dbo.Numbers VALUES(16.354); INSERT dbo.Numbers VALUES(16.355); INSERT dbo.Numbers VALUES(16.356); INSERT dbo.Numbers VALUES(16.357); INSERT dbo.Numbers VALUES(16.358); INSERT dbo.Numbers VALUES(16.359); INSERT dbo.Numbers VALUES(16.360); GO SELECT Number , CAST(Number - 0.001 AS decimal(4, 2)) AS Arrotondamento1 , ROUND(Number - 0.001, 2) AS Arrotondamento2 FROM dbo.Numbers; GO /* Output: Number Arrotondamento1 Arrotondamento2 ------- ---------------- ---------------- 16.350 16.35 16.350 16.351 16.35 16.350 16.352 16.35 16.350 16.353 16.35 16.350 16.354 16.35 16.350 16.355 16.35 16.350 16.356 16.36 16.360 16.357 16.36 16.360 16.358 16.36 16.360 16.359 16.36 16.360 16.360 16.36 16.360 (11 row(s) affected) */ DROP TABLE dbo.Numbers;

>Ciao e grazie ancora.
Prego.

Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5