...

View Full Version : How to customize ThousandSeparator to display price in INR format



dhanasekaran
07-28-2009, 10:12 AM
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

Inigoesdr
07-30-2009, 12:41 PM
You have to extend the currency formatter, and write a custom thousands' seperator function. Here is how I did it:




<?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:




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;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum