Code:
/*
Format class.
Copyright 2005, Bruce A. Epstein. All Rights Reserved.
Modificata per AS3 il 05/05/2009
*/
package azero.utils
{
public class CurrencyNumber
{
// Constructor, not used. All methods are static.
private var thousandsDelim:String;
private var decimalDelim:String;
private var spaceFill:Number;
public function numberFormat(num:Number, _thousandsDelim:String=",", _decimalDelim:String=".", _spaceFill:Number=1):String
{
thousandsDelim = _thousandsDelim;
decimalDelim = _decimalDelim;
spaceFill = _spaceFill;
var val:String;
var origLength:Number;
var counter:Number = 0;
// Convert the number to a string and split it at the decimal point.
var parts:Array = String(num).split(".");
// Take the whole number portion and stores it as an array of single characters.
// This makes it easier to insert the thousands delimiter, as needed.
var partOneAr:Array = parts[0].split("");
// Reverse the array so we can process the characters right to left.
partOneAr.reverse( );
// Insert the thousands delimiter after every third character.
for (var i:Number=0; i < partOneAr.length; i++)
{
counter++;
if (counter > 3)
{
counter = 0;
partOneAr.splice(i, 0, thousandsDelim);
}
}
// Reverse the array again so that it is back in the original order.
partOneAr.reverse();
// Creo la formattazione usando i decimalDelim, se necessario (234,45).
val = partOneAr.join("");
//parts[1] sono i valori dopo la virgola
if (parts[1] != null)
{
val += decimalDelim + parts[1];
}
// If spaceFill is defined, add the necessary number of leading spaces.
// Se definsco spaceFill, aggiungo gli spazi numerici principali
if (spaceFill)
{
// Store the original length before adding spaces.
origLength=val.length;
trace("origLength",origLength);
for (var j:Number = 0; j < spaceFill - origLength; j++)
{
val=" "+val;
trace(j);
}
}
// Return the value.
return val;
}
// The method converts a numeric parameter into a currency-formatted string.
//roundDecPl( ), numberFormat( ), and zeroFill( ) methods on which this example relies.
public function currencyFormat(_num:Number, decimalPl:Number, currencySymbol:String, thousandsDelim:String, decimalDelim:String, truncate:Boolean, spaceFill:Number):String
{
trace("currencyFormat",currencyFormat);
var num:Number=_num;
var tempNum:Number=num;
// Default to two decimal places, a dollar sign ($), a comma for thousands, and a
// period for the decimal point. We implemented the defaults using the conditional
// operator. Compare with Recipe 5.5.
decimalPl = (!decimalPl) ? 2 : decimalPl;
currencySymbol = (!currencySymbol) ? "$" : currencySymbol;
thousandsDelim = (!thousandsDelim) ? "," : thousandsDelim;
decimalDelim = (!decimalDelim) ? "." : decimalDelim;
/*
if (truncate)
{
//num=tempNum.toFixed(decimalPl);
trace("toFixed",num.toFixed(decimalPl));// 7.313
}*/
// Split the number into the whole and decimal (fractional) portions.
var parts=String(num).split(".");
// If there is no decimal delimeter, assume zero after the decimal
if (parts.length<2)
{
parts[1]="0";
}
// Truncate or round the decimal portion, as directed.
if (truncate)
{
parts[1] = Number(parts[1]) * Math.pow(10, -(decimalPl - 1));
parts[1]=String(Math.floor(parts[1]));
trace("parts[1]",parts[1])
}
else
{
parts[1]=roundDecPl(Number("."+parts[1]),decimalPl);
var temp:Array=String(parts[1]).split(".");
// If there is no decimal delimeter, assume zero after the decimal
if (temp.length<2)
{
parts[1]="0";
}
else
{
parts[1]=temp[1];
}
}
// Ensure that the decimal portion has the number of digits indicated.
parts[1]=zeroFill(parts[1],decimalPl,true);
// Format the number with the proper thousands delimiter and leading spaces.
if (thousandsDelim!=""||! spaceFill)
{
parts[0]=numberFormat(parts[0],thousandsDelim,"",spaceFill-decimalPl-currencySymbol.length);
}
// Add a currency symbol and use String.join( ) to merge the whole (dollar) and
// decimal (cents) portions using the designated decimal delimiter.
trace(currencySymbol + parts.join(decimalDelim));
return currencySymbol + parts.join(decimalDelim);
}
// rounds a number to a specified number of decimal places.
public static function roundDecPl(num:Number, decPl:Number):Number
{
// decPl defaults to 0 (round to the nearest integer).
if (! decPl)
{
decPl=0;
}
// Calculate the value used to multiply the original number,
// which is 10 raised to the power of decPl.
var multiplier=Math.pow(10,decPl);
// Return the result.
return Math.round(num * multiplier) / multiplier;
}
public static function zeroFill(inputNum:String, places:Number, trailing:Boolean):String
{
// Assume the input "number" is really a String, so we can format, say, binary numbers.
var filledVal:String;
if (inputNum=="")
{
filledVal="0";
}
else
{
filledVal=inputNum;
}
// Get the length of the string.
var len:Number=filledVal.length;
// Use a for statement to add the necessary number of characters.
for (var i:Number = 0; i < (places - len); i++)
{
// If trailing is true, append the zeros; otherwise, prepend them.
if (trailing)
{
filledVal+="0";
}
else
{
filledVal="0"+filledVal;
}
}
// Return the string.
return filledVal;
}
}
}
Bookmarks