Vous n'êtes pas identifié(e).
Pages : 1
Bonjour,
je suis à la recherche du'une fonction d'arrondi supérieur qui me donne le résultat suivant:
round(123.121958,2) = 123.13
Dans excel ça existe mais PG, je ne la trouve pas!
Merci
Hors ligne
à mon avis, vous allez devoir passer par un procédure stockée maison ...
Hors ligne
Ok, merci!
Hors ligne
Si quelqu'un a une piste pour compter le nombre de chiffre après une virgule je suis preneur.
Je suis également preneur de toute indication qui pourra m'aider à écrire la procédure stockée évoquée par arthur.
Hors ligne
Testé chez moi :
CREATE OR REPLACE function special_round(p_to_round decimal,p_nbre_decimal integer) RETURNS decimal AS $$
DECLARE
	p_return decimal;
BEGIN
	IF(round(p_to_round,p_nbre_decimal)<p_to_round) THEN
		p_to_round := p_to_round + 9 /10^(p_nbre_decimal+1);
	END IF;
	p_return := round(p_to_round,p_nbre_decimal);
	RETURN p_return;
END;
$$ LANGUAGE plpgsql;Tests :
test=# select * from special_round(123.121958,2);
 special_round 
---------------
        123.13
(1 row)
test=# select * from special_round(123.120,3);
 special_round 
---------------
       123.120
(1 row)Hors ligne
Merci arthur!
Hors ligne
SELECT arrondir_sup(1.00000009,2) ;
arrondir_sup
-----------------
             1.01
alors que le résultat attendu est 1.10
Hors ligne
j'ai du mal à suivre ...
premier exemple que tu donnes :
round(123.121958,2) = 123.13puis :
SELECT arrondir_sup(1.00000009,2) ;Ce dernier exemple demande 2 chiffre après la virgule arrondi au supérieur, donc 1.01
Pour moi, si tu veux 1.10, tu fais :
postgres=# SELECT special_round(1.00000009,1);
 special_round 
---------------
           1.1Hors ligne
OK merci, je me suis vautré.
Hors ligne
Je mets la solution que j'ai utilisé pour ceux que ça intéresserait!
CREATE OR REPLACE FUNCTION arrondir_sup_type_excel ( prix_htc numeric) RETURNS numeric AS
$$
DECLARE
   v_prix numeric;
   S_QUERY    text;
   v_digit    text;
BEGIN
   v_prix := trunc(prix_htc,3);
S_QUERY := 'SELECT SUBSTR('||quote_literal(v_prix) || ',char_length(' || quote_literal(v_prix) ||'),char_length('|| quote_literal(v_prix) ||'))';
   EXECUTE S_QUERY INTO v_digit;
  IF v_digit = '0' THEN
    RETURN trunc(v_prix,2);
  ELSE
    RETURN trunc(v_prix,2) +0.01;
  END IF;
END
$$
LANGUAGE 'plpgsql'
STABLE
;Comme j'ai affaire à des utilisateurs ils m'ont suggérés  que dans excel arrondir_sup(1.00000009,2) = 1.10.
Alors que pour moi c'est bien 1.01.
Merci arthurr pour tes réponses.
Hors ligne
Pages : 1