AsadYarKhan
AsadYarKhan

Reputation: 688

How to Convert Exponentiation Number into Fixed Decimal Number in Adobe Flex3

How to convert exponential to fixed decimal. example: 2.5e12 ,0.12323e-11 etc

Upvotes: 0

Views: 1034

Answers (2)

AsadYarKhan
AsadYarKhan

Reputation: 688

public function formatIntoFixedValue(value:Object):Object
            {
                var isNegativeNum:Boolean ;
                var isNegativeExp:Boolean;
                var isDecimal:Boolean;
                var splitNums:Array;
                var splitFixedNum:Array;
                var leftDecPlaceCount:Number=0;
                var rightDecPlaceCount:Number=0;
                var exponentNum=0;
                var finalNum:String;
                var valueNum:Number=Number(value);
                var valueStr:String = valueNum.toString();

// is not exponent return

            if(valueStr.indexOf("e")<0)
            return valueStr;

            if(valueNum<0)

// Number is negative or not

                isNegativeNum=true;
            else
                isNegativeNum=false;

            splitFixedNum= valueStr.split("e");

            if(Number(splitFixedNum[1])<0)

//Exponent is negative or not

                isNegativeExp=true;
            else 
                isNegativeExp=false;

            exponentNum=Math.abs(Number(splitFixedNum[1]));//-7

// removing space
splitFixedNum[0]=StringUtil.trimArrayElements(splitFixedNum[0],"");

// removing minus sign from start if exist. splitFixedNum[0]=splitFixedNum[0].toString().replace("-","");

// it is removing zeors of start

            finalNum=Number(splitFixedNum[0]).toString(); 

// Is Decimal or Not

            if(finalNum.toString().indexOf(".")>=0)
            {
            isDecimal=true;
            }
            else 
            isDecimal=false;

/// exponent is zero

            if(exponentNum==0)
            return finalNum;

// if num is decimal if(isDecimal) { var expDiff:Number;

                splitNums= String(splitFixedNum[0]).split(".");

                splitNums[0]=Number(splitNums[0]).toString();
                if(splitNums[0]!="0")
                {



                finalNum=splitNums[0].toString().concat(splitNums[1]); 
                leftDecPlaceCount=splitNums[0].toString().length;

                }
                else
                {



                finalNum=splitNums[1]; 
                leftDecPlaceCount=0;
                }


                rightDecPlaceCount=splitNums[1].toString().length;

                if(isNegativeExp)
                    expDiff=exponentNum-leftDecPlaceCount;

                else
                    expDiff=exponentNum-rightDecPlaceCount;

//If exp diff is greater than zero concatenate zeros

                if(expDiff>0)
                {
                        var zeros:String="";

                        for(var i:Number=0;i<expDiff;i++)
                            zeros+="0";

                        if(isNegativeExp)   
                        finalNum="0."+zeros+finalNum;
                        else
                        finalNum= finalNum+zeros;   

                }

//If exp diff is less than zero than move .(dot) between number

                else if(expDiff<0)
                {
                    var s:String;
                    expDiff= Math.abs(expDiff);
                    if(isNegativeExp)
                    s =finalNum.substr(0,expDiff)+"."+finalNum.substr(expDiff,finalNum.length);
                    else
                    s =finalNum.substr(0,(leftDecPlaceCount+rightDecPlaceCount)-expDiff)+"."+finalNum.substr((leftDecPlaceCount+rightDecPlaceCount)-expDiff,finalNum.length);
                    finalNum=s;
                }
                else
                {
                    if(isNegativeExp)
                    finalNum= "0."+finalNum;

                }
            }
            else
            {

                        var zeros:String="";

                        for(var i:Number=0;i<exponentNum-finalNum.length;i++)
                            zeros+="0";

                        if(isNegativeExp)   
                        finalNum="0."+zeros+finalNum;
                        else
                        finalNum= finalNum+zeros;
            }   

            if(isNegativeNum)
            finalNum ="-"+ finalNum;

            return finalNum.toString();
        }

Upvotes: 0

Sunil D.
Sunil D.

Reputation: 18193

In Flash/Flex you can use one of the NumberFormatter classes. Below is an example using the Flex 3 (MX) NumberFormatter. Take a look at the docs for the API's each one offers, they are a little different from each other.

var n:Number = 3 * .0000000001;
trace(n); // 3e-10
var f:NumberFormatter = new NumberFormatter();
f.precision=10;
trace(f.format(n)); // 0.0000000003

There are 3 NumberFormatter classes, one for pure Flash (non-Flex), one for Flex 3, and the Flex 4 version. The first and last ones will also format the number specific to the user's locale (use comma's instead of periods, where appropriate, etc.).

Here are the links to documentation:

Upvotes: 1

Related Questions