mahe1987 Posted July 20, 2010 Share Posted July 20, 2010 Hi, I am using a variable, i want to print the number in words for that variable Please help mePost Edited by mahe1987 at 07/20/2010 10:47 Link to comment Share on other sites More sharing options...
slow Posted July 20, 2010 Share Posted July 20, 2010 can you be more clear? can you give examples? Link to comment Share on other sites More sharing options...
mahe1987 Posted July 21, 2010 Author Share Posted July 21, 2010 i am having a field which is number type, if it returns 200 then i need to print this 200 in another variable or filed as Two hundred, similarly if 2000 then it should print two thousand etc. Link to comment Share on other sites More sharing options...
slow Posted July 21, 2010 Share Posted July 21, 2010 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 beAnd I can see things I know I really shouldn't seeAnd now I know why now, now I know whyThings 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 More sharing options...
mahe1987 Posted July 21, 2010 Author Share Posted July 21, 2010 thanx for reply i gave u karma points Link to comment Share on other sites More sharing options...
bostabox Posted June 28, 2018 Share Posted June 28, 2018 Aftet I convert the numbers to text. How I can make it looks like that\Only seven thousands, two hundred ninety four pound and fifty eight piasters\ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now