OVH Cloud OVH Cloud

[FLA] Nombre avec x décimales

2 réponses
Avatar
Iam Noone
Bonjour à tous,
Comment afficher une valeur avec x décimales ?
Exemple : 12,14785 donne 12,15 ?
Merci !

2 réponses

Avatar
Philippe Nomail webforumsuser
En Flash MX 2004 - d'après les sources de mx.data.formatters.NumberFormatter
et avec les commentaires buggés ;-)

function format(value:Number, precision:Number):String {
var strValue:String;
if( precision > 0 ) {
var perc:Number = Math.pow( 10, precision );
strValue = ( Math.round( value * perc ) / perc ).toString();
// check to be sure that we have the correct format
if( strValue.length > 0 ) {
var index = strValue.lastIndexOf( '.' );
var zCnt = 0;
if( index < 0 ) {
strValue += ".";
zCnt = precision;
}
else
zCnt = this.precision -( strValue.length- ( index +1 ));
// add the zeros on
strValue = strValue + "0000000000000000000000000000".substring( 0, zCnt );
} // if
}
else
strValue = Math.round( value ).toString(); // trunc to 0 percision

return strValue;
}

var a = 12.14785;

trace(" format 0 = "+format(a,0));
trace(" format 1 = "+format(a,1));
trace(" format 2 = "+format(a,2));
trace(" format 3 = "+format(a,3));

Philippe
http://philflash.inway.fr
Avatar
David The Dolphin
MonNombre = Math.round(MonAncienNombre*100)/100

--
DTD
---
Quand j'étais jeune, j'étais con. Je suis resté très jeune. (JC. Van Damme)
----


"Iam Noone" a écrit dans le message de
news:bqft7i$1a5$
Bonjour à tous,
Comment afficher une valeur avec x décimales ?
Exemple : 12,14785 donne 12,15 ?
Merci !