Conversor de cantidades a letra (SPANISH)

import java.math.RoundingMode; import java.text.DecimalFormat; public abstract class ConversorCantidades { private static final String[] UNIDADES = {"", "UN ", "DOS ", "TRES ", "CUATRO ", "CINCO ", "SEIS ", "SIETE ", "OCHO ", "NUEVE ", "DIEZ ", "ONCE ", "DOCE ", "TRECE ", "CATORCE ", "QUINCE ", "DIECISEIS", "DIECISIETE", "DIECIOCHO", "DIECINUEVE", "VEINTE"}; private static final String[] DECENAS = {"VENTI", "TREINTA ", "CUARENTA ", "CINCUENTA ", "SESENTA ", "SETENTA ", "OCHENTA ", "NOVENTA ", "CIEN "}; private static final String[] CENTENAS = {"CIENTO ", "DOSCIENTOS ", "TRESCIENTOS ", "CUATROCIENTOS ", "QUINIENTOS ", "SEISCIENTOS ", "SETECIENTOS ", "OCHOCIENTOS ", "NOVECIENTOS "}; public static String convertNumberToLetter(String number) throws NumberFormatException { return convertNumberToLetter(Double.parseDouble(number)); } public static String convertNumberToLetter(double doubleNumber) throws NumberFormatException { StringBuilder converted = new StringBuilder(); String patternThreeDecimalPoints = "#.###"; DecimalFormat format = new DecimalFormat(patternThreeDecimalPoints); format.setRoundingMode(RoundingMode.DOWN); // formateamos el numero, para ajustarlo a el formato de tres puntos // decimales String formatedDouble = format.format(doubleNumber); doubleNumber = Double.parseDouble(formatedDouble); // Validamos que sea un numero valido if (doubleNumber > 999999999) throw new NumberFormatException( "El numero es mayor de 999'999.999, " + "no es posible convertirlo"); if (doubleNumber < 0) throw new NumberFormatException("El numero debe ser positivo"); String splitNumber[] = String.valueOf(doubleNumber).replace('.', '#') .split("#"); // Descompone el trio de millones int millon = Integer.parseInt(String.valueOf(getDigitAt(splitNumber[0], 8)) + String.valueOf(getDigitAt(splitNumber[0], 7)) + String.valueOf(getDigitAt(splitNumber[0], 6))); if (millon == 1) converted.append("UN MILLON "); else if (millon > 1) converted.append(convertNumber(String.valueOf(millon))).append("MILLONES "); // Descompone el trio de miles int miles = Integer.parseInt(String.valueOf(getDigitAt(splitNumber[0], 5)) + String.valueOf(getDigitAt(splitNumber[0], 4)) + String.valueOf(getDigitAt(splitNumber[0], 3))); if (miles == 1) converted.append("MIL "); else if (miles > 1) converted.append(convertNumber(String.valueOf(miles))).append("MIL "); // Descompone el ultimo trio de unidades int cientos = Integer.parseInt(String.valueOf(getDigitAt( splitNumber[0], 2)) + String.valueOf(getDigitAt(splitNumber[0], 1)) + String.valueOf(getDigitAt(splitNumber[0], 0))); if (cientos == 1) converted.append("UN"); if (millon + miles + cientos == 0) converted.append("CERO"); if (cientos > 1) converted.append(convertNumber(String.valueOf(cientos))); converted.append("PESOS"); // Descompone los centavos int centavos = Integer.parseInt(String.valueOf(getDigitAt( splitNumber[1], 2)) + String.valueOf(getDigitAt(splitNumber[1], 1)) + String.valueOf(getDigitAt(splitNumber[1], 0))); if (centavos == 1) converted.append(" CON UN CENTAVO"); else if (centavos > 1) converted.append(" CON ").append(convertNumber(String.valueOf(centavos))).append("CENTAVOS"); return converted.toString(); } private static String convertNumber(String number) { if (number.length() > 3) throw new NumberFormatException( "La longitud maxima debe ser 3 digitos"); // Caso especial con el 100 if (number.equals("100")) { return "CIEN"; } StringBuilder output = new StringBuilder(); if (getDigitAt(number, 2) != 0) output.append(CENTENAS[getDigitAt(number, 2) - 1]); int k = Integer.parseInt(String.valueOf(getDigitAt(number, 1)) + String.valueOf(getDigitAt(number, 0))); if (k <= 20) output.append(UNIDADES[k]); else if (k > 30 && getDigitAt(number, 0) != 0) output.append(DECENAS[getDigitAt(number, 1) - 2]).append("Y ").append(UNIDADES[getDigitAt(number, 0)]); else output.append(DECENAS[getDigitAt(number, 1) - 2]).append(UNIDADES[getDigitAt(number, 0)]); return output.toString(); } private static int getDigitAt(String origin, int position) { if (origin.length() > position && position >= 0) return origin.charAt(origin.length() - position - 1) - 48; return 0; } }
Convierte una cantidad a letra (en español).

8 Responses

gracias!!
Gracias por compartir!
Gracias... de mucha ayuda.... Thanks very helpful
Muy buena, gracias.
Hola, me podrías decir la razón por la que definiste la clase de forma abstracta?
Si los métodos son estáticos, no creo que necesites declarar abstracta la clase, con 10000000 el programa comete un error, está muy bueno el código, pero hay que hacerle algunas modificaciones.
Qué buen trabajo!
https://gitlab.wacren.net/snippets/815
https://woremsay.gumroad.com/p/the-single-best-strategy-to-use-for-master-writing-revealed
http://ivt.bluesoleil.com/forum/thread-2-56847-1.html
https://projects.growthhackers.com/cards/professional-writer/4M52R5XFryhmhnxB9JhwIA
http://cs.astronomy.com/members/professional-writer/default.aspx
https://informatics.msk.ru/user/profile.php?id=648935
https://soc.cungcap.net/read-blog/7231_unanswered-questions-into-writing-resource-revealed.html
https://letterboxd.com/quiranim/
http://www.rohitab.com/discuss/user/137830-snaupban/
http://tiagarelmtiagr.eklablog.com/the-ultimate-guide-to-creative-writer-that-you-can-learn-about-today-a209456294
http://www.metalstorm.net/users/noawight/profile
https://mastodon.social/@Anna_Delilah
https://bikepgh.org/message-board/users/nehinche/
https://www.turnkeylinux.org/user/1550628
https://www.pckitcj.com/posts/list/0/27112.page#40987
https://ryderbrayden.coffeecup.com/article1.html
https://www.pixnet.net/pcard/4286860e2bc5323a29/article/cd1df700-dd67-11eb-aca9-fb5ae16f1521?utm_source=PIXNET&utm_medium=pcard_article&utm_content=4286860e2bc5323a29
https://canvas.mooc.upc.edu/eportfolios/5252/Home/Master_Writing__Most_Vital_Tips
https://forum.abantecart.com/index.php?action=profile;area=summary;u=45700
http://colleye.96.lt/members/jonahriver/buddyblog/my-posts/18686/
https://ashton-tylers-stunning-project.webflow.io/specific-facts-associated-with-writing-resource
https://community.waveapps.com/profile/Ivy_Kinsley
http://mocyc.com/board/35469/LearnAboutVariousConceptsAboutCreativeWritingOnline
http://www.digthedirt.com/contributions/review/277251-Writing-And-Blogging-Is-Must-For-Everyone
https://wozyzy.com/read-blog/5359
https://fortunetelleroracle.com/education/essay-writer-321970
https://myworldgo.com/blog/14204/good-number-of-reviews-before-using-professional-writer
https://pubhtml5.com/homepage/ptsb
https://git.qt.io/Remi_Margaret
http://inktalks.com/people/maxwellemmanuel
https://www.intensedebate.com/people/Thiago_Camden
https://www.emoneyspace.com/alexgraham
https://essaywritingservices.teamapp.com/articles/5760170-the-truth-on-creative-writing-online?_detail=v1&_expires_at=1630454399
https://www.methodspace.com/members/liam/profile/
http://www.letmetalk.info/discussions/how-you-can-take-benefit-out-of-writing-and-blogging-online.html
https://canund.com/read-blog/24421
https://artistecard.com/KylieElliana
https://neverbroke.club/read-blog/42110
https://muckrack.com/ronan-maximiliano/bio
http://www.biznas.com/Biz-postsm128588_Proper-And-Valuable-Knowledge-About-Creative-Writer.aspx#post128588
https://app.socie.com.br/read-blog/25514
https://www.vevioz.com/read-blog/15504
http://dev.infotechnologist.biz/motor/blogs/10954/Highly-Important-Factors-AboutCreative-Writing-Online
https://rabbitroom.com/members/williamjames-2/profile/
https://topgradeapp.com/lesson/what-makes-creative-writer-so-special
https://www.wishlistr.com/profile/Oliverbenjamin
https://www.nj24.pl/users/holdenremington
https://www.theloop.com.au/project/Mark_Brantley/portfolio/have-you-seriously-considered-the-option-of-creative-writing-online/402780
https://www.webmasterpro.de/user/Ariella_Esther/
https://www.oaklandperio.com/User-Profile/userId/305143
https://60e9ad62cea5c.site123.me/blog/the-well-known-facts-about-professional-writer
https://openlibrary.org/people/khloetaylor
https://gotartwork.com/Profile/alexander-lucas/97755/
https://coderwall.com/Callie_Sienna
https://www.amtamembers.com/blakelykehlani.edit?return_url=https%3A%2F%2Fwww.amtamembers.com%2FPages%2FEdit%2FFull%2F317149
https://logopond.com/AlinaBailey/profile/492360/
https://starity.hu/profil/294253-bradley-damien/
https://www.anime-planet.com/users/ZanderHendrix
https://completed.com/individual/30521012/malakai-malcolm
https://reveeo.com/blogs/creative_writing_online_just_enhance_your_knowledge_now_/1
https://www.recentstatus.com/read-blog/16898_the-effective-role-of-writing-resource.html
https://www.americanframe.com/artist/georgia-magnolia.aspx
https://emilyisabella.webform.com/form/36702
https://www.worknplay.co.kr/Story/Detail/Article/11371
http://www.nissanownersclub.wix.ph/groups/26731
http://community.getvideostream.com/topic/6798/writing-and-blogging-is-popular-worldwide-due-to-following-reasons
https://community.onsen.io/user/tiagarelmtiagr
https://www.9020club.com/read-blog/5317
https://www.mifare.net/support/forum/users/june_sloane/
https://fnetchat.com/read-blog/18102
https://macro.market/company/rebecca-thomas
https://www.voubs.com/group/event/view/writing/ef798078dedbbb5dc4594bdb69d43494/221
http://vetiverhairspa.com/UserProfile/tabid/807/userId/659020/Default.aspx
https://www.hoomet.com/forums/topic/22964/writing-resource-huge-opportunity-to-succeed/view/post_id/33963
https://fairmark.com/forum/users/paisley_brooklyn/
https://selfieoo.com/read-blog/13420
https://www.heredis.com/forums/en/viewtopic.php?f=18&t=6584
http://mortchalfy.com/UserProfile/tabid/217/userId/453123/Default.aspx
https://calis.delfi.lv/blogs/posts/56711-master-writing-have-lot-to-offer-so-you-must-check-the-out/lietotajs/238229-anna-caroline/
http://atlas.dustforce.com/user/Alivia_Raegan
https://openuserjs.org/users/Ruby_Valentina
https://twitback.com/read-blog/37174
https://uconnect.ae/read-blog/37948
https://www.skreebee.com/read-blog/24236
https://www.mojomarketplace.com/user/sarah_josephine-4zu2go0tc3
https://hackmd.io/@essaywriting/HyUf4-2AO
https://www.lrngo.com/class/876
https://kontactr.com/form-page/6bed5f4f1f3843d
https://alicegabriella.splashthat.com/
https://www.channelfocuscommunity.net/forum/general-channel-discussion/392-the-insider-secrets-of-writing-resource-discovered
https://git.project-hobbit.eu/snippets/2996
https://invertnet.org/members/8052/blog/2021/07/the-unexplained-mystery-into-master-writing-uncovered
https://bitbucket.org/melanie-melody-writing/workspace/snippets/gBnMnz
https://degentevakana.com/blogs/view/37780
https://ekonty.com/read-blog/2182
https://teletype.in/@rose_reagan/OP8g-abeGFW
https://www.notion.so/Why-Nobody-Is-Talking-About-Creative-Writing-Online-And-What-You-Should-Do-Today-47e65ae8a05e4876bcebed23fd74a9d2
https://pharmahub.org/members/10218/blog/2021/07/rumors-lies-and-master-writing
https://www.colcampus.com/eportfolios/40089/Home/The_Undeniable_Truth_About_Master_Writing
https://www.discussagile.com/forums/users/irisparker
https://www.haikudeck.com/the-most-overlooked-fact-about-best-writer-revealed-education-presentation-bec09f4b4b

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.