- fixed bug with amount of 1.00 showing " /00" instead of "UNO/00"

- fixed bug to support both point and comma as the decimal markers and thousands group markers
- fixed bug with amount of just decimals (like .00) giving error
- fixed bug with amounts without decimals (like 1200) giving error
- changed type from int to long to support amounts greater than 2,147,483,647
- added a print method to simplify tests
- changed main method to test all supported cases

Link to SF Tracker: http://sourceforge.net/support/tracker.php?aid=2990865
This commit is contained in:
genied 2010-04-22 13:01:10 +00:00
parent 79a1decdde
commit e265d7fd27
1 changed files with 43 additions and 20 deletions

View File

@ -134,7 +134,7 @@ public class AmtInWords_IT implements AmtInWords
* @param number * @param number
* @return amt * @return amt
*/ */
private String convert (int number) private String convert (long number)
{ {
/* special case */ /* special case */
if (number == 0) if (number == 0)
@ -153,7 +153,7 @@ public class AmtInWords_IT implements AmtInWords
if (n != 0) if (n != 0)
{ {
String s = convertLessThanOneThousand ((int)n); String s = convertLessThanOneThousand ((int)n);
if (n == 1) if (n == 1 && place > 0)
soFar = majorNames[place] + soFar; soFar = majorNames[place] + soFar;
else else
soFar = s + majorNamesPlural[place] + soFar; soFar = s + majorNamesPlural[place] + soFar;
@ -177,7 +177,8 @@ public class AmtInWords_IT implements AmtInWords
{ {
if (amount == null) if (amount == null)
return amount; return amount;
//
// assume rightmost point or comma as decimal separator
StringBuffer sb = new StringBuffer (); StringBuffer sb = new StringBuffer ();
int pos = amount.lastIndexOf (','); int pos = amount.lastIndexOf (',');
int pos2 = amount.lastIndexOf ('.'); int pos2 = amount.lastIndexOf ('.');
@ -185,33 +186,55 @@ public class AmtInWords_IT implements AmtInWords
pos = pos2; pos = pos2;
String oldamt = amount; String oldamt = amount;
// strips decimals from the amount
if(pos >= 0)
amount = amount.substring (0, pos);
// remove points and commas
amount = amount.replaceAll( "\\.",""); amount = amount.replaceAll( "\\.","");
amount = amount.replaceAll( ",","");
long amt = amount.length() > 0 ? Long.parseLong(amount) : 0;
int newpos = amount.lastIndexOf (',');
int amt = Integer.parseInt (amount.substring (0, newpos));
sb.append (convert (amt)); sb.append (convert (amt));
for (int i = 0; i < oldamt.length (); i++) if(pos >= 0)
{ {
if (pos == i) // we are done String cents = oldamt.substring (pos + 1);
{ sb.append ("/").append (cents);
String cents = oldamt.substring (i + 1);
sb.append ("/").append (cents);
break;
}
} }
return sb.toString (); return sb.toString ();
} // getAmtInWords } // getAmtInWords
/**
* Test Print
* @param amt amount
*/
private void print (String amt)
{
try
{
System.out.println(amt + " = " + getAmtInWords(amt));
}
catch (Exception e)
{
e.printStackTrace();
}
} // print
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
AmtInWords_IT aiw = new AmtInWords_IT(); AmtInWords_IT aiw = new AmtInWords_IT();
System.out.println(aiw.getAmtInWords("0,00")); aiw.print(",00"); // no error, assume zero (same as next)
System.out.println(aiw.getAmtInWords("1000,00")); aiw.print("0,00");
System.out.println(aiw.getAmtInWords("14000,99")); aiw.print("1000,00");
System.out.println(aiw.getAmtInWords("28000000,99")); aiw.print("1200");
System.out.println(aiw.getAmtInWords("301000000,00")); aiw.print("1.200"); // no error but ambiguous for currencies with no decimals
System.out.println(aiw.getAmtInWords("200000,99")); aiw.print("14000.99");
System.out.println(aiw.getAmtInWords("-1234567890,99")); aiw.print("28.000.000,99"); // points as thousands separators, comma for decimals
System.out.println(aiw.getAmtInWords("2147483647,99")); aiw.print("301,000,000.00"); // commas as thousands separators, point for decimals
aiw.print("200000,99");
aiw.print("-1234567890,99"); // negative amount
aiw.print("2147483647,99");
aiw.print("9223372036854775807.99"); // very big amount
} }
} // AmtInWords_IT } // AmtInWords_IT