IDEMPIERE-3471 Fix amount in words for Brazilian Portuguese / Thanks to Ricardo Santana (ralexsander)

This commit is contained in:
Carlos Ruiz 2017-09-08 14:52:25 +02:00
parent e3690e803b
commit 01124f8ee4
1 changed files with 230 additions and 135 deletions

View File

@ -1,6 +1,5 @@
/****************************************************************************** /******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution * * Product: ADempiereLBR - ADempiere Localization Brazil *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it * * This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published * * under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope * * by the Free Software Foundation. This program is distributed in the hope *
@ -10,18 +9,18 @@
* You should have received a copy of the GNU General Public License along * * You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., * * with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/ *****************************************************************************/
package org.compiere.util; package org.compiere.util;
import java.math.BigDecimal;
/** /**
* Portuguese Amount in Words * Portuguese Amount in Words
* *
* @author Jorg Janke - http://www.rgagnon.com/javadetails/java-0426.html * @author Jorg Janke - http://www.rgagnon.com/javadetails/java-0426.html
* @translator emontenegro * @translator emontenegro, ralexsander
* @version $Id: AmtInWords_PT.java,v 1.0 2008/01/07 00:54:36 jjanke Exp $ * @contributor mgrigioni - UTF-8 Code
* @version $Id: AmtInWords_PT.java,v 1.0 2008/01/07 00:54:36 ralexsander Exp $
*/ */
public class AmtInWords_PT implements AmtInWords public class AmtInWords_PT implements AmtInWords
{ {
@ -33,24 +32,48 @@ public class AmtInWords_PT implements AmtInWords
super (); super ();
} // AmtInWords_PT } // AmtInWords_PT
private static final String[] centsNames = {
"",
"",
"",
" D\u00e9cimo de",
" Cent\u00e9simo de",
" Mil\u00e9simo de",
" Milion\u00e9simo de",
" Bilion\u00e9simo de",
" Trilion\u00e9simo de"
};
private static final String[] centsNamesPlural = {
"",
"",
"",
" D\u00e9cimos de",
" Cent\u00e9simos de",
" Mil\u00e9simos de",
" Milion\u00e9simos de",
" Bilion\u00e9simos de",
" Trilion\u00e9simos de"
};
private static final String[] majorNames = { private static final String[] majorNames = {
"", "",
" Mil", " Mil",
" Milhão", " Milh\u00e3o",
" Bilhão", " Bilh\u00e3o",
" Trilhão", " Trilh\u00e3o",
" Quatrilhão", " Quatrilh\u00e3o",
" Quinquilhão" " Quinquilh\u00e3o"
}; };
private static final String[] majorNamesPlural = { private static final String[] majorNamesPlural = {
"", "",
" Mil", " Mil",
" Milhões", " Milh\u00f5es",
" Bilhões", " Bilh\u00f5es",
" Trilhões", " Trilh\u00f5es",
" Quatrilhões", " Quatrilh\u00f5es",
" Quinquilhões" " Quinquilh\u00f5es"
}; };
private static final String[] tensNames = { private static final String[] tensNames = {
@ -59,7 +82,7 @@ public class AmtInWords_PT implements AmtInWords
" Vinte", " Vinte",
" Trinta", " Trinta",
" Quarenta", " Quarenta",
" Cinqüenta", " Cinq\u00fcenta",
" Sessenta", " Sessenta",
" Setenta", " Setenta",
" Oitenta", " Oitenta",
@ -70,7 +93,7 @@ public class AmtInWords_PT implements AmtInWords
"", "",
" Um", " Um",
" Dois", " Dois",
" Três", " Tr\u00eas",
" Quatro", " Quatro",
" Cinco", " Cinco",
" Seis", " Seis",
@ -111,7 +134,7 @@ public class AmtInWords_PT implements AmtInWords
if (s.endsWith ("2") && !soFar.equals("")) if (s.endsWith ("2") && !soFar.equals(""))
soFar = " Vinte e " + soFar.trim (); soFar = " Vinte e " + soFar.trim ();
else if (soFar.equals("")) else if (soFar.equals(""))
soFar = tensNames[number % 10] + " e" + soFar; soFar = tensNames[number % 10] + soFar;
else else
soFar = tensNames[number % 10] + " e" + soFar; soFar = tensNames[number % 10] + " e" + soFar;
number /= 10; number /= 10;
@ -134,29 +157,31 @@ public class AmtInWords_PT implements AmtInWords
* @param number * @param number
* @return amt * @return amt
*/ */
private String convert (int number) private String convert (BigDecimal number)
{ {
/* special case */ /* special case */
if (number == 0) if (number.compareTo(Env.ZERO) == 0)
return ""; return "";
if (number == 1) if (number.compareTo(Env.ONE) == 0)
return "Um"; return "Um";
if (number == -1) if (number.compareTo(Env.ONE.negate()) == 0)
return "Menos Um"; return "Menos Um";
String prefix = ""; String prefix = "";
if (number < 0) if (number.compareTo(Env.ZERO) == -1)
{ {
number = -number; number = number.negate();
prefix = "Menos"; prefix = "Menos";
} }
if (number > 1000000 && number < 2000000){
/*if ((number >= 1000000 && number < 2000000)
|| (number >= 1000000000 && number < 2000000000)){
prefix = "Um"; prefix = "Um";
} }*/
String soFar = ""; String soFar = "";
int place = 0; int place = 0;
do do
{ {
int n = number % 1000; int n = number.divideAndRemainder(new BigDecimal(1000))[1].intValue();/// % 1000;
if (n != 0) if (n != 0)
{ {
String s = convertLessThanOneThousand (n); String s = convertLessThanOneThousand (n);
@ -168,9 +193,9 @@ public class AmtInWords_PT implements AmtInWords
{ {
s = s.replaceFirst ("Dois Cento es", "Duzentos"); s = s.replaceFirst ("Dois Cento es", "Duzentos");
} }
if (s.startsWith ("Três Cento es", 1)) if (s.startsWith ("Tr\u00eas Cento es", 1))
{ {
s = s.replaceFirst ("Três Cento es", "Trezentos"); s = s.replaceFirst ("Tr\u00eas Cento es", "Trezentos");
} }
if (s.startsWith ("Quatro Cento es", 1)) if (s.startsWith ("Quatro Cento es", 1))
{ {
@ -198,23 +223,39 @@ public class AmtInWords_PT implements AmtInWords
} }
if (s.equals(" Um")) if (s.equals(" Um"))
{ {
soFar = majorNames[place] + soFar; soFar = s + majorNames[place] + (soFar.equals("") ? "" : " e" + soFar);
} }
else { else {
if (n > 1) { if (n > 1)
soFar = s + majorNamesPlural[place] + soFar; {
} else { soFar = s + majorNamesPlural[place] + (soFar.equals("") ? "" : " e" + soFar);
soFar = s + majorNames[place] + soFar; //soFar = s + majorNamesPlural[place] + soFar;
} }
else
{
soFar = s + majorNames[place] + (soFar.equals("") ? "" : " e" + soFar);
}
} }
} }
place++; place++;
number /= 1000; number = number.divideAndRemainder(new BigDecimal(1000))[0];
} }
while (number > 0); while (number.compareTo(Env.ZERO) == 1);
return (prefix + soFar).trim (); return (prefix + soFar)
.replaceAll(" e Mil", " Mil")
.trim ();
} // convert } // convert
public String getAmtInWords (BigDecimal amount) throws Exception
{
amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP);
String samount = amount.toString();
samount = samount.replaceAll("\\.", ",");
return getAmtInWords(samount);
}
/************************************************************************** /**************************************************************************
* Get Amount in Words * Get Amount in Words
@ -227,130 +268,184 @@ public class AmtInWords_PT implements AmtInWords
if (amount == null) if (amount == null)
return amount; return amount;
// //
StringBuilder sb = new StringBuilder (); StringBuffer sb = new StringBuffer ();
// int pos = amount.lastIndexOf ('.'); // Old int pos = amount.lastIndexOf ('.'); // Old
int pos = amount.lastIndexOf (','); // int pos = amount.lastIndexOf (',');
// int pos2 = amount.lastIndexOf (','); // Old int pos2 = amount.lastIndexOf (','); // Old
int pos2 = amount.lastIndexOf ('.'); // int pos2 = amount.lastIndexOf ('.');
if (pos2 > pos) if (pos2 > pos)
pos = pos2; pos = pos2;
String oldamt = amount; String oldamt = amount;
// amount = amount.replaceAll (",", ""); // Old amount = amount.replaceAll ("\\.", "").replaceAll (",", "."); // Old
String vlr = amount.replaceAll (",", "."); String vlr = amount.replaceAll (",", ".");
amount = amount.replaceAll( "\\.","");
// int newpos = amount.lastIndexOf ('.'); // Old int newpos = amount.lastIndexOf ('.'); // Old
int newpos = amount.lastIndexOf (','); // int newpos = amount.lastIndexOf (',');
int pesos = Integer.parseInt (amount.substring (0, newpos)); if (newpos == -1) newpos = amount.length();
BigDecimal reais = new BigDecimal(amount.substring (0, newpos));
double valor = Double.parseDouble(vlr); double valor = Double.parseDouble(vlr);
sb.append (convert (pesos)); sb.append (convert (reais));
for (int i = 0; i < oldamt.length (); i++) for (int i = 0; i < oldamt.length (); i++)
{ {
if (pos == i) // we are done if (pos == i) // we are done
{ {
String cents = oldamt.substring (i + 1); String cents = oldamt.substring (i + 1);
if (valor > 0 && valor < 1) { do
if (Integer.parseInt(cents) > 0)
{ {
if (Integer.parseInt(cents) > 1) if(cents.endsWith("0") && cents.length() > 2)
cents = cents.substring(0, cents.length() -1);
}
while (cents.endsWith("0") && cents.length() > 2);
if (valor > 0 && valor < 1)
{
if (Integer.parseInt(cents) > 0)
{ {
//sb.append (' ') if (Integer.parseInt(cents) > 1)
//.append("e ") {
sb.append (convert(Integer.parseInt(cents))) sb.append (convert(new BigDecimal(cents)))
.append(" Centavos"); .append(" Centavos");
// .append ("/100"); }
// .append ("/100 PESOS"); else
{
sb.append (convert(new BigDecimal(cents)))
.append(" Centavo");
}
} }
else {
//sb.append (' ')
//.append("e ")
sb.append (convert(Integer.parseInt(cents)))
.append(" Centavo");
// .append ("/100");
// .append ("/100 PESOS");
}
}
}
else if ((valor > 1 && valor < 2) || (valor > -2 && valor < -1)){
if (Integer.parseInt(cents) > 0)
{
if (Integer.parseInt(cents) > 1)
{
sb.append (' ')
.append("Real e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavos");
// .append ("/100");
// .append ("/100 PESOS");
} }
else { else if ((valor > 1 && valor < 2) || (valor > -2 && valor < -1))
sb.append (' ')
.append("Real e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavo");
// .append ("/100");
// .append ("/100 PESOS");
}
break;
}
}
else if (valor > -1 && valor < 0){
if (Integer.parseInt(cents) > 0)
{ {
if (Integer.parseInt(cents) > 1) if (Integer.parseInt(cents) > 0)
{ {
sb.append ("Menos ") if (Integer.parseInt(cents) > 1)
// .append("Real e ") {
.append (convert(Integer.parseInt(cents))) sb.append (' ')
.append(" Centavos"); .append("Real e ")
// .append ("/100"); .append (convert(new BigDecimal(cents)))
// .append ("/100 PESOS"); .append(" Centavos");
}
else
{
sb.append (' ')
.append("Real e ")
.append (convert(new BigDecimal(cents)))
.append(" Centavo");
}
break;
} }
else {
sb.append ("Menos ")
//.append("Real e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavo");
// .append ("/100");
// .append ("/100 PESOS");
}
break;
} }
} else if (valor > -1 && valor < 0)
else {
if (Integer.parseInt(cents) > 0)
{ {
if (Integer.parseInt(cents) > 1) if (Integer.parseInt(cents) > 0)
{ {
sb.append (' ') if (Integer.parseInt(cents) > 1)
.append("Reais e ") {
.append (convert(Integer.parseInt(cents))) sb.append ("Menos ")
.append(" Centavos"); .append (convert(new BigDecimal(cents)))
// .append ("/100"); .append(centsNamesPlural[cents.length()])
// .append ("/100 PESOS"); .append(centsNamesPlural[cents.length()].equals("") ? " Centavos" : " Centavo");
}
else
{
sb.append ("Menos ")
.append (convert(new BigDecimal(cents)))
.append(centsNames[cents.length()])
.append(" Centavo");
}
break;
} }
else {
sb.append (' ')
.append("Reais e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavo");
// .append ("/100");
// .append ("/100 PESOS");
}
break;
} }
else
{
if (!cents.equals("") && Integer.parseInt(cents) > 0)
{
if (Integer.parseInt(cents) > 1)
{
sb.append (' ')
.append("Reais e ")
.append (convert(new BigDecimal(cents)))
.append(centsNamesPlural[cents.length()])
.append(centsNamesPlural[cents.length()].equals("") ? " Centavos" : " Centavo");
}
else
{
sb.append (' ')
.append("Reais e ")
.append (convert(new BigDecimal(cents)))
.append(centsNames[cents.length()])
.append(" Centavo");
}
break;
}
else
{
if (reais.abs().compareTo(Env.ONE) == 0)
sb.append(" Real");
else
sb.append(" Reais");
}
} }
} }
} }
return sb.toString (); if (pos == -1){
if (reais.abs().compareTo(Env.ONE) == 0)
sb.append(" Real");
else
sb.append(" Reais");
}
/** Corre\u00e7\u00f5es */
String result;
result = sb.toString ()
.replaceAll(" e Quinquilh", " Quinquilh")
.replaceAll(" e Quatrilh", " Quatrilh")
.replaceAll(" e Trilh", " Trilh")
.replaceAll(" e Bilh", " Bilh")
.replaceAll(" e Milh", " Milh")
.replaceAll("\u00f5es Reais", "\u00f5es de Reais")
.replaceAll("\u00e3o Reais", "\u00e3o de Reais")
.replaceAll(" e Reais", " Reais")
.replaceAll(", de", " de");
if (result.indexOf("Quinquilh") > 0 && result.indexOf("Quatrilh") > 0 && result.indexOf("Quatrilh\u00f5es de Rea") == -1 && result.indexOf("Quatrilh\u00e3o de Rea") == -1)
result = result.replaceAll("Quinquilh\u00f5es e", "Quinquilh\u00f5es,").replaceAll("Quinquilh\u00e3o e", "Quinquilh\u00e3o,");
if (result.indexOf("Quatrilh") > 0 && result.indexOf("Trilh") > 0 && result.indexOf("Trilh\u00f5es de Rea") == -1 && result.indexOf("Trilh\u00e3o de Rea") == -1)
result = result.replaceAll("Quatrilh\u00f5es e", "Quatrilh\u00f5es,").replaceAll("Quatrilh\u00e3o e", "Quatrilh\u00e3o,");
if (result.indexOf("Trilh") > 0 && result.indexOf("Bilh") > 0 && result.indexOf("Bilh\u00f5es de Rea") == -1 && result.indexOf("Bilh\u00e3o de Rea") == -1)
result = result.replaceAll("Trilh\u00f5es e", "Trilh\u00f5es,").replaceAll("Trilh\u00e3o e", "Trilh\u00e3o,");
if (result.indexOf("Bilh") > 0 && result.indexOf("Milh") > 0 && result.indexOf("Milh\u00f5es de Rea") == -1 && result.indexOf("Milh\u00e3o de Rea") == -1)
result = result.replaceAll("Bilh\u00f5es e", "Bilh\u00f5es,").replaceAll("Bilh\u00e3o e", "Bilh\u00e3o,");
if (result.indexOf("Milh") > 0 && result.indexOf(" de Rea") == -1 && result.indexOf("Mil e") > 0)
result = result.replaceAll("Milh\u00f5es e", "Milh\u00f5es,").replaceAll("Milh\u00e3o e", "Milh\u00e3o,");
return result ;
} // getAmtInWords } // getAmtInWords
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception
{
AmtInWords_PT aiw = new AmtInWords_PT(); AmtInWords_PT aiw = new AmtInWords_PT();
// for (int i=0; i<=2147000000; i++) System.out.println(aiw.getAmtInWords("42"));
// System.out.println(aiw.getAmtInWords(i+",00")); System.out.println(aiw.getAmtInWords("0,42"));
System.out.println(aiw.getAmtInWords("134502932,01")); System.out.println(aiw.getAmtInWords("100"));
System.out.println(aiw.getAmtInWords("100,00"));
System.out.println(aiw.getAmtInWords("1003,00"));
System.out.println(aiw.getAmtInWords("5715,13"));
System.out.println(aiw.getAmtInWords("5715,11"));
System.out.println(aiw.getAmtInWords("5715,20"));
System.out.println(aiw.getAmtInWords("5715,30"));
System.out.println(aiw.getAmtInWords("5715,44"));
System.out.println(aiw.getAmtInWords("5715,55"));
System.out.println(aiw.getAmtInWords("5715,60"));
System.out.println(aiw.getAmtInWords("5715,79"));
System.out.println(aiw.getAmtInWords("5715,82"));
System.out.println(aiw.getAmtInWords("5715,90"));
System.out.println(aiw.getAmtInWords(new BigDecimal(51.34)));
} }
} // AmtInWords_PT } // AmtInWords_PT