Jump to content

how to print number in words using field/variable


mahe1987

Recommended Posts

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

It's possible using a library you set in you classpath that uses a class that converts numbers to words... or a report scriplet with the same methods...

I think also this is a java-forum retated question... not an iReport-forum one...

but... here to you a class that makes what you want... simply include it in a jar and put this jar in your IReport classpath or put the class in a scriplet:

 

public class NumberToWords{
         private static final String[] tens = {
                                                "",
                                                " ten",
                                                " twenty",
                                                " thirty",
                                                " forty",
                                                " fifty",
                                                " sixty",
                                                " seventy",
                                                " eighty",
                                                " ninety"
          };

          private static final String[] numbers = {
                                                "",
                                                " one",
                                                " two",
                                                " three",
                                                " four",
                                                " five",
                                                " six",
                                                " seven",
                                                " eight",
                                                " nine",
                                                " ten",
                                                " eleven",
                                                " twelve",
                                                " thirteen",
                                                " fourteen",
                                                " fifteen",
                                                " sixteen",
                                                " seventeen",
                                                " eighteen",
                                                " nineteen"
          };

          private static String convertNumberLessThanOneThousand(int number) {
            String rest;

            if (number % 100 < 20){
              rest = numbers[number % 100];
              number /= 100;
            }
            else {
              rest = numbers[number % 10];
              number /= 10;

              rest = tens[number % 10] + rest;
              number /= 10;
            }
            if (number == 0) return rest;
            return numbers[number] + " hundred" + rest;
          }


          public static String convertNumber(long number) {
            if (number == 0) { return "zero"; }

            String snumber = Long.toString(number);

            String zeroMask = "000000000000";
            DecimalFormat df = new DecimalFormat(zeroMask);
            snumber = df.format(number);

            int billions = Integer.parseInt(snumber.substring(0,3));
            int millions  = Integer.parseInt(snumber.substring(3,6));
            int hundredThousands = Integer.parseInt(snumber.substring(6,9));
            int thousands = Integer.parseInt(snumber.substring(9,12));   

            String billionsString;
            switch (billions) {
            case 0:
                billionsString = "";
              break;
            case 1 :
                billionsString = convertNumberLessThanOneThousand(billions)
              + " billion ";
              break;
            default :
                billionsString = convertNumberLessThanOneThousand(billions)
              + " billion ";
            }
            String result =  billionsString;

            String millionsString;
            switch (millions) {
            case 0:
                millionsString = "";
              break;
            case 1 :
                millionsString = convertNumberLessThanOneThousand(millions)
              + " million ";
              break;
            default :
                millionsString = convertNumberLessThanOneThousand(millions)
              + " million ";
            }
            result =  result + millionsString;

            String hundredThousandsString;
            switch (hundredThousands) {
            case 0:
                hundredThousandsString = "";
              break;
            case 1 :
                hundredThousandsString = "one thousand ";
              break;
            default :
                hundredThousandsString = convertNumberLessThanOneThousand(hundredThousands)
              + " thousand ";
            }
            result =  result + hundredThousandsString;

            String thousandString;
            thousandString = convertNumberLessThanOneThousand(thousands);
            result =  result + thousandString;

            return result.replaceAll("^\s+", "").replaceAll("\b\s{2,}\b", " ");
          }

 

to use the conversion set the String textField expression as:

(yourclassname).convertNumber($F{yourNumericFiledName}.longValue())

or if you use a scriplet:

$P{REPORT_SCRIPTLET}.convertNumber($F{yourNumericFiledName}.longValue())

 

_________________________________________

if you like it... give me KARMA points please!    : ) 
_________________________________________

listening:  Nine Inch Nails Only

"Now I'm somewhere I am not supposed to be
And I can see things I know I really shouldn't see
And now I know why now, now I know why
Things aren't as pretty on the inside" (t.r.)
 

 



Post Edited by slow at 07/21/2010 08:09
Link to comment
Share on other sites

  • 7 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...