Go Back   CodingForums.com > :: Client side development > Flash & ActionScript > Adobe Flex

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-28-2009, 10:12 AM   PM User | #1
dhanasekaran
New to the CF scene

 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dhanasekaran is an unknown quantity at this point
Smile How to customize ThousandSeparator to display price in INR format

All,


I need to format the below amount in indian rupee(INR) format.


Amount: 10000000.00


When i use the default ThousandSeparator it return the amount as follows:

<mx:NumberFormatter id="numberFormatter" precision="2" useThousandsSeparator="true"
thousandsSeparatorFrom="," thousandsSeparatorTo=","/>


numberFormatter.format(Amount) -------> 1,000,000,000.00 --------> it's in US dollar format


i want it as,


numberFormatter.format(Amount) -------> 1,00,00,00,000.00 --------> it's in INR rupee format


Please some one tell me how to do this,


by,

Dhanasekaran
dhanasekaran is offline   Reply With Quote
Old 07-30-2009, 12:41 PM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
You have to extend the currency formatter, and write a custom thousands' seperator function. Here is how I did it:
Quote:
Originally Posted by main.mxml
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
	xmlns:mx="http://www.adobe.com/2006/mxml"
	xmlns:fm="com.CustomFormatters.*"
	xmlns="*">

<fm:INRFormatter id="inrFormatter" precision="2" 
    currencySymbol="" decimalSeparatorFrom="."
    decimalSeparatorTo="." useNegativeSign="true" 
    useThousandsSeparator="true" alignSymbol="left" />
        
<mx:Canvas>
	<mx:Text text="{inrFormatter.format('123456789')}" />
</mx:Canvas>

</mx:Application>
And here is the class for doing the formatting:
Quote:
Originally Posted by com/CustomFormatters/INRFormatter.as
Code:
package com.CustomFormatters
{
	import mx.formatters.*;

	public class INRFormatter extends CurrencyFormatter
	{
		public function INRFormatter()
		{
			super();
		}

		/**
		 *  Formats a number by using 
		 *  the <code>thousandsSeparatorTo</code> property as the thousands separator 
		 *  and the <code>decimalSeparatorTo</code> property as the decimal separator.
		 *
		 *  @param value Value to be formatted.
		 *
		 *  @return Formatted number.
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 9
		 *  @playerversion AIR 1.1
		 *  @productversion Flex 3
		 */
		public function formatThousands(value:String):String
		{
			var v:Number = Number(value);
			
			var isNegative:Boolean = (v < 0);
			
			var numStr:String = Math.abs(v).toString();
			var numArr:Array =
				numStr.split((numStr.indexOf(decimalSeparatorTo) != -1) ? decimalSeparatorTo : ".");
			var numLen:int = String(numArr[0]).length;

			if(numLen > 3)
			{
				numLen -= 3;
				var arr:Array = [];
				if (numLen > 2)
				{
					var numSep:int = int(Math.floor(numLen / 2));
	
					if ((numLen % 2) == 0)
						numSep--;

					var b:int = numLen;
					var a:int = b - 2;
					
					for (var i:int = 0; i <= numSep; i++)
					{
						arr[i] = numArr[0].slice(a, b);
						a = int(Math.max(a - 2, 0));
						b = int(Math.max(b - 2, 1));
					}
					
					arr.reverse();
					arr[i] = numStr.substr(-3, 3);
				}
				else if (numLen == 2)
				{
					arr = [numStr.substr(0,2), numStr.substr(2)]
				}
				else
				{
					arr = [numStr.substr(0,1), numStr.substr(1)]
				}
				numArr[0] = arr.join(thousandsSeparatorTo);
			}
			
			numStr = numArr.join(decimalSeparatorTo);
			
			if (isNegative)
				numStr = "-" + numStr;
			
			return numStr.toString();
		}

	
		/**
	     *  Formats <code>value</code> as currency.
		 *  If <code>value</code> cannot be formatted, return an empty String 
		 *  and write a description of the error to the <code>error</code> property.
		 *
	     *  @param value Value to format.
		 *
	     *  @return Formatted string. Empty if an error occurs.
	     *  
	     *  @langversion 3.0
	     *  @playerversion Flash 9
	     *  @playerversion AIR 1.1
	     *  @productversion Flex 3
	     */
	    override public function format(value:Object):String
	    {
	        // Reset any previous errors.
	        if (error)
				error = null;
	
	        if (useThousandsSeparator &&
				(decimalSeparatorFrom == thousandsSeparatorFrom ||
				 decimalSeparatorTo == thousandsSeparatorTo))
	        {
	            error = defaultInvalidFormatError;
	            return "";
	        }
	
	        if (decimalSeparatorTo == "")
	        {
	            error = defaultInvalidFormatError;
	            return "";
	        }
	
	        var dataFormatter:NumberBase = new NumberBase(decimalSeparatorFrom,
														  thousandsSeparatorFrom,
														  decimalSeparatorTo,
														  thousandsSeparatorTo);
	
	        // -- value --
	
	        if (value is String)
	            value = dataFormatter.parseNumberString(String(value));
	
	        if (value === null || isNaN(Number(value)))
	        {
	            error = defaultInvalidValueError;
	            return "";
	        }
	
	        // -- format --
	        
			var isNegative:Boolean = (Number(value) < 0);
	
	        var numStr:String = value.toString();
	        var numArrTemp:Array = numStr.split(".");
	        var numFraction:int = numArrTemp[1] ? String(numArrTemp[1]).length : 0;
	
	        if (precision <= numFraction)
			{
	            if (rounding != NumberBaseRoundType.NONE)
				{
	                numStr = dataFormatter.formatRoundingWithPrecision(
						numStr, rounding, int(precision));
				}
			}

	        var numValue:Number = Number(numStr);
	        if (Math.abs(numValue) >= 1)
	        {
	            numArrTemp = numStr.split(".");
	            var front:String =
					useThousandsSeparator ?
					formatThousands(String(numArrTemp[0])) :
					String(numArrTemp[0]);
	            if (numArrTemp[1] != null && numArrTemp[1] != "")
	                numStr = front + decimalSeparatorTo + numArrTemp[1];
	            else
	                numStr = front;
	        }
	        else if (Math.abs(numValue) > 0)
	        {
	        	// if the value is in scientefic notation then the search for '.' 
	        	// doesnot give the correct result. Adding one to the value forces 
	        	// the value to normal decimal notation. 
	        	// As we are dealing with only the decimal portion we need not 
	        	// worry about reverting the addition
	        	if (numStr.indexOf("e") != -1)
	        	{
		        	var temp:Number = Math.abs(numValue) + 1;
		        	numStr = temp.toString();
	        	}
	            numStr = decimalSeparatorTo +
						 numStr.substring(numStr.indexOf(".") + 1);
	        }

	        numStr = dataFormatter.formatPrecision(numStr, int(precision));

			// If our value is 0, then don't show -0
			if (Number(numStr) == 0)
			{
				isNegative = false;	
			}
	
	        if (isNegative)
	            numStr = dataFormatter.formatNegative(numStr, useNegativeSign);
	
	        if (!dataFormatter.isValid)
	        {
	            error = defaultInvalidFormatError;
	            return "";
	        }
	
	        // -- currency --
	
	        if (alignSymbol == "left")
			{
	            if (isNegative)
				{
	                var nSign:String = numStr.charAt(0);
	                var baseVal:String = numStr.substr(1, numStr.length - 1);
	                numStr = nSign + currencySymbol + baseVal;
	            }
				else
				{
	                numStr = currencySymbol + numStr;
	            }
	        } 
			else if (alignSymbol == "right")
			{
	            var lastChar:String = numStr.charAt(numStr.length - 1);
	            if (isNegative && lastChar == ")")
				{
	                baseVal = numStr.substr(0, numStr.length - 1);
	                numStr = baseVal + currencySymbol + lastChar;
	            }
				else
				{
	                numStr = numStr + currencySymbol;
	            }
	        }
			else
			{
	            error = defaultInvalidFormatError;
	            return "";
	        }
	
	        return numStr;
	    }
	}
}
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 PM.


Advertisement
Log in to turn off these ads.