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 *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* Product: ADempiereLBR - ADempiere Localization Brazil *
* 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 *
* 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 *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 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;
import java.math.BigDecimal;
/**
* Portuguese Amount in Words
*
* @author Jorg Janke - http://www.rgagnon.com/javadetails/java-0426.html
* @translator emontenegro
* @version $Id: AmtInWords_PT.java,v 1.0 2008/01/07 00:54:36 jjanke Exp $
* @translator emontenegro, ralexsander
* @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
{
@ -33,24 +32,48 @@ public class AmtInWords_PT implements AmtInWords
super ();
} // 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 = {
"",
" Mil",
" Milhão",
" Bilhão",
" Trilhão",
" Quatrilhão",
" Quinquilhão"
" Milh\u00e3o",
" Bilh\u00e3o",
" Trilh\u00e3o",
" Quatrilh\u00e3o",
" Quinquilh\u00e3o"
};
private static final String[] majorNamesPlural = {
"",
" Mil",
" Milhões",
" Bilhões",
" Trilhões",
" Quatrilhões",
" Quinquilhões"
" Milh\u00f5es",
" Bilh\u00f5es",
" Trilh\u00f5es",
" Quatrilh\u00f5es",
" Quinquilh\u00f5es"
};
private static final String[] tensNames = {
@ -59,7 +82,7 @@ public class AmtInWords_PT implements AmtInWords
" Vinte",
" Trinta",
" Quarenta",
" Cinqüenta",
" Cinq\u00fcenta",
" Sessenta",
" Setenta",
" Oitenta",
@ -70,7 +93,7 @@ public class AmtInWords_PT implements AmtInWords
"",
" Um",
" Dois",
" Três",
" Tr\u00eas",
" Quatro",
" Cinco",
" Seis",
@ -111,7 +134,7 @@ public class AmtInWords_PT implements AmtInWords
if (s.endsWith ("2") && !soFar.equals(""))
soFar = " Vinte e " + soFar.trim ();
else if (soFar.equals(""))
soFar = tensNames[number % 10] + " e" + soFar;
soFar = tensNames[number % 10] + soFar;
else
soFar = tensNames[number % 10] + " e" + soFar;
number /= 10;
@ -134,29 +157,31 @@ public class AmtInWords_PT implements AmtInWords
* @param number
* @return amt
*/
private String convert (int number)
private String convert (BigDecimal number)
{
/* special case */
if (number == 0)
if (number.compareTo(Env.ZERO) == 0)
return "";
if (number == 1)
if (number.compareTo(Env.ONE) == 0)
return "Um";
if (number == -1)
if (number.compareTo(Env.ONE.negate()) == 0)
return "Menos Um";
String prefix = "";
if (number < 0)
if (number.compareTo(Env.ZERO) == -1)
{
number = -number;
number = number.negate();
prefix = "Menos";
}
if (number > 1000000 && number < 2000000){
/*if ((number >= 1000000 && number < 2000000)
|| (number >= 1000000000 && number < 2000000000)){
prefix = "Um";
}
}*/
String soFar = "";
int place = 0;
do
{
int n = number % 1000;
int n = number.divideAndRemainder(new BigDecimal(1000))[1].intValue();/// % 1000;
if (n != 0)
{
String s = convertLessThanOneThousand (n);
@ -168,9 +193,9 @@ public class AmtInWords_PT implements AmtInWords
{
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))
{
@ -198,22 +223,38 @@ public class AmtInWords_PT implements AmtInWords
}
if (s.equals(" Um"))
{
soFar = majorNames[place] + soFar;
soFar = s + majorNames[place] + (soFar.equals("") ? "" : " e" + soFar);
}
else {
if (n > 1) {
soFar = s + majorNamesPlural[place] + soFar;
} else {
soFar = s + majorNames[place] + soFar;
}
if (n > 1)
{
soFar = s + majorNamesPlural[place] + (soFar.equals("") ? "" : " e" + soFar);
//soFar = s + majorNamesPlural[place] + soFar;
}
else
{
soFar = s + majorNames[place] + (soFar.equals("") ? "" : " e" + soFar);
}
}
}
place++;
number /= 1000;
number = number.divideAndRemainder(new BigDecimal(1000))[0];
}
while (number > 0);
return (prefix + soFar).trim ();
while (number.compareTo(Env.ZERO) == 1);
return (prefix + soFar)
.replaceAll(" e Mil", " Mil")
.trim ();
} // 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);
}
/**************************************************************************
@ -227,130 +268,184 @@ public class AmtInWords_PT implements AmtInWords
if (amount == null)
return amount;
//
StringBuilder sb = new StringBuilder ();
// int pos = amount.lastIndexOf ('.'); // Old
int pos = amount.lastIndexOf (',');
// int pos2 = amount.lastIndexOf (','); // Old
int pos2 = amount.lastIndexOf ('.');
StringBuffer sb = new StringBuffer ();
int pos = amount.lastIndexOf ('.'); // Old
// int pos = amount.lastIndexOf (',');
int pos2 = amount.lastIndexOf (','); // Old
// int pos2 = amount.lastIndexOf ('.');
if (pos2 > pos)
pos = pos2;
String oldamt = amount;
// amount = amount.replaceAll (",", ""); // Old
amount = amount.replaceAll ("\\.", "").replaceAll (",", "."); // Old
String vlr = amount.replaceAll (",", ".");
amount = amount.replaceAll( "\\.","");
// int newpos = amount.lastIndexOf ('.'); // Old
int newpos = amount.lastIndexOf (',');
int pesos = Integer.parseInt (amount.substring (0, newpos));
int newpos = amount.lastIndexOf ('.'); // Old
// int newpos = amount.lastIndexOf (',');
if (newpos == -1) newpos = amount.length();
BigDecimal reais = new BigDecimal(amount.substring (0, newpos));
double valor = Double.parseDouble(vlr);
sb.append (convert (pesos));
sb.append (convert (reais));
for (int i = 0; i < oldamt.length (); i++)
{
if (pos == i) // we are done
{
String cents = oldamt.substring (i + 1);
if (valor > 0 && valor < 1) {
if (Integer.parseInt(cents) > 0)
String cents = oldamt.substring (i + 1);
do
{
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 (' ')
//.append("e ")
sb.append (convert(Integer.parseInt(cents)))
.append(" Centavos");
// .append ("/100");
// .append ("/100 PESOS");
if (Integer.parseInt(cents) > 1)
{
sb.append (convert(new BigDecimal(cents)))
.append(" Centavos");
}
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 {
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)
else if ((valor > 1 && valor < 2) || (valor > -2 && valor < -1))
{
if (Integer.parseInt(cents) > 1)
if (Integer.parseInt(cents) > 0)
{
sb.append ("Menos ")
// .append("Real e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavos");
// .append ("/100");
// .append ("/100 PESOS");
if (Integer.parseInt(cents) > 1)
{
sb.append (' ')
.append("Real e ")
.append (convert(new BigDecimal(cents)))
.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 (Integer.parseInt(cents) > 0)
else if (valor > -1 && valor < 0)
{
if (Integer.parseInt(cents) > 1)
if (Integer.parseInt(cents) > 0)
{
sb.append (' ')
.append("Reais e ")
.append (convert(Integer.parseInt(cents)))
.append(" Centavos");
// .append ("/100");
// .append ("/100 PESOS");
if (Integer.parseInt(cents) > 1)
{
sb.append ("Menos ")
.append (convert(new BigDecimal(cents)))
.append(centsNamesPlural[cents.length()])
.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
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception
{
AmtInWords_PT aiw = new AmtInWords_PT();
// for (int i=0; i<=2147000000; i++)
// System.out.println(aiw.getAmtInWords(i+",00"));
System.out.println(aiw.getAmtInWords("134502932,01"));
System.out.println(aiw.getAmtInWords("42"));
System.out.println(aiw.getAmtInWords("0,42"));
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