You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Enke Michael <Mi...@wincor-nixdorf.com> on 2001/09/07 15:04:21 UTC

I18nTransformer patch

Hi folks,
here is a patch for I18nTransformer:
1) new tag int-currency introduced:
   In merchandise calculations often
   the calculations are done not with double
   ($1,23+$4,56) but with integers:
   (cent 123+cent 456)
   Soo give the value 579 will result
   in 5.79 for US locale but 579 for spanish locale
   because there is no sub currency in Spain.
2) the <i18n:number> tag did not preserve
   trailing and leading zeros and did only show
   three decimal places.
   Soo <i18n:number>0.12345</i18n:number>
   was transformed to 0.123
   and <i18n:number>0.10000</i18n:number>
   was transformed to 0.1
   This is now fixed.
Could a commiter commit please?

Regards,
Michael


Re: I18nTransformer patch

Posted by Davanum Srinivas <di...@yahoo.com>.
Done. Please cross-check both branches.
-- dims
--- Enke Michael <Mi...@wincor-nixdorf.com> wrote:
> Hi folks,
> here is a patch for I18nTransformer:
> 1) new tag int-currency introduced:
>    In merchandise calculations often
>    the calculations are done not with double
>    ($1,23+$4,56) but with integers:
>    (cent 123+cent 456)
>    Soo give the value 579 will result
>    in 5.79 for US locale but 579 for spanish locale
>    because there is no sub currency in Spain.
> 2) the <i18n:number> tag did not preserve
>    trailing and leading zeros and did only show
>    three decimal places.
>    Soo <i18n:number>0.12345</i18n:number>
>    was transformed to 0.123
>    and <i18n:number>0.10000</i18n:number>
>    was transformed to 0.1
>    This is now fixed.
> Could a commiter commit please?
> 
> Regards,
> Michael
> 
> > --- I18nTransformer.java.orig	Thu Sep  6 15:46:30 2001
> +++ I18nTransformer.java	Fri Sep  7 12:57:30 2001
> @@ -258,7 +258,8 @@
>  
>      /**
>       * <code>sub-type</code> attribute is used with <code>i18:number</code> to
> -     * indicate a sub-type: <code>currency</code> or <code>percent</code>.
> +     * indicate a sub-type: <code>currency</code>, <code>int-currency</code>
> +     * or <code>percent</code>.
>       */
>      public static final String I18N_SUB_TYPE_ATTRIBUTE = "sub-type";
>  
> @@ -1058,6 +1059,8 @@
>  
>          // src format
>          DecimalFormat from_fmt = (DecimalFormat)NumberFormat.getInstance(srcLoc);
> +	int int_currency = 0;
> +
>  	// src-pattern overwrites locale format
>          if (srcPattern != null) {
>              from_fmt.applyPattern(srcPattern);
> @@ -1065,10 +1068,28 @@
>  
>  	// to format
>          DecimalFormat to_fmt = null;
> +        char dec = from_fmt.getDecimalFormatSymbols().getDecimalSeparator();
> +        int decAt = 0;
> +        boolean appendDec = false;
>          if (subType == null) {
>              to_fmt = (DecimalFormat)NumberFormat.getInstance(loc);
> +            to_fmt.setMaximumFractionDigits(309);
> +            for(int i=value.length()-1;
> +                i>=0 && value.charAt(i)!=dec;i--,decAt++);
> +            if(decAt < value.length()) to_fmt.setMinimumFractionDigits(decAt);
> +            decAt = 0;
> +            for(int i = 0; i < value.length() && value.charAt(i) != dec; i++) {
> +              if(Character.isDigit(value.charAt(i))) decAt++;
> +            }
> +            to_fmt.setMinimumIntegerDigits(decAt);
> +            if(value.charAt(value.length()-1) == dec) appendDec = true;
>          } else if (subType.equals("currency")) {
>              to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc);
> +        } else if (subType.equals("int-currency")) {
> +            to_fmt = (DecimalFormat)NumberFormat.getCurrencyInstance(loc);
> +	    int_currency = 1;
> +	    for(int i=0;i<to_fmt.getMaximumFractionDigits();i++)
> +		int_currency *= 10;
>          } else if (subType.equals("percent")) {
>              to_fmt = (DecimalFormat)NumberFormat.getPercentInstance(loc);
>          }
> @@ -1083,6 +1104,12 @@
>          } else {
>              try {
>                  numberValue = from_fmt.parse(value);
> +                if(int_currency > 0)
> +		    numberValue = new Double(numberValue.doubleValue()/
> +					     int_currency);
> +                else {
> +
> +                }
>              } catch (ParseException pe) {
>                  throw new SAXException(this.getClass().getName()
>                                         + "i18n:number - parsing error.", pe);
> @@ -1091,6 +1118,7 @@
>  
>          // we have all necessary data here: do formatting.
>          String result = to_fmt.format(numberValue);
> +        if(appendDec) result = result + dec;
>          debug("i18n:number result: " + result);
>          return result;
>      }
> > --- i18n-transformer.xml.orig	Fri Sep  7 14:50:02 2001
> +++ i18n-transformer.xml	Fri Sep  7 14:49:35 2001
> @@ -189,6 +189,7 @@
>   				</p>
>  					<ul>
>  						<li><code><![CDATA[<i18n:number sub-type="currency" value="1703.74" />]]></code> will
> result in localized presentation of the <code>value</code> - $1,703.74 for US locale.</li>
> +						<li><code><![CDATA[<i18n:number sub-type="int-currency" value="170374" />]]></code> will
> result in localized presentation of the <code>value</code> - $1,703.74 for US locale, 170374 for
> a currency without subunit.</li>
>  						<li><code><![CDATA[<i18n:number sub-type="percent" value="1.2" />]]></code> will result
> in localized percent <code>value</code> - %120 for most of the locales.</li>
>  					</ul>
>  				<p>
> 
> > ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org


=====
Davanum Srinivas, JNI-FAQ Manager
http://www.jGuru.com/faq/JNI

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org