Convert Number to Indian Currency in Java Java by Rajesh Kumar Sahanee - October 10, 2017April 14, 20200 Post Views: 18,436 Hello Friends, In my previous post, I shared code that was for converting number to Indian currency in PHP and today I am going to share how to convert number to Indian currency in Java. With some little changes same previous code logic can be used to develop code to convert number to indian currency in java. Here is the code Currency.java Java import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; /** * * @author rajesh kumar sahanee */ public class Currency { public static String convertToIndianCurrency(String num) { BigDecimal bd = new BigDecimal(num); long number = bd.longValue(); long no = bd.longValue(); int decimal = (int) (bd.remainder(BigDecimal.ONE).doubleValue() * 100); int digits_length = String.valueOf(no).length(); int i = 0; ArrayList<String> str = new ArrayList<>(); HashMap<Integer, String> words = new HashMap<>(); words.put(0, ""); words.put(1, "One"); words.put(2, "Two"); words.put(3, "Three"); words.put(4, "Four"); words.put(5, "Five"); words.put(6, "Six"); words.put(7, "Seven"); words.put(8, "Eight"); words.put(9, "Nine"); words.put(10, "Ten"); words.put(11, "Eleven"); words.put(12, "Twelve"); words.put(13, "Thirteen"); words.put(14, "Fourteen"); words.put(15, "Fifteen"); words.put(16, "Sixteen"); words.put(17, "Seventeen"); words.put(18, "Eighteen"); words.put(19, "Nineteen"); words.put(20, "Twenty"); words.put(30, "Thirty"); words.put(40, "Forty"); words.put(50, "Fifty"); words.put(60, "Sixty"); words.put(70, "Seventy"); words.put(80, "Eighty"); words.put(90, "Ninety"); String digits[] = {"", "Hundred", "Thousand", "Lakh", "Crore"}; while (i < digits_length) { int divider = (i == 2) ? 10 : 100; number = no % divider; no = no / divider; i += divider == 10 ? 1 : 2; if (number > 0) { int counter = str.size(); String plural = (counter > 0 && number > 9) ? "s" : ""; String tmp = (number < 21) ? words.get(Integer.valueOf((int) number)) + " " + digits[counter] + plural : words.get(Integer.valueOf((int) Math.floor(number / 10) * 10)) + " " + words.get(Integer.valueOf((int) (number % 10))) + " " + digits[counter] + plural; str.add(tmp); } else { str.add(""); } } Collections.reverse(str); String Rupees = String.join(" ", str).trim(); String paise = (decimal) > 0 ? " And Paise " + words.get(Integer.valueOf((int) (decimal - decimal % 10))) + " " + words.get(Integer.valueOf((int) (decimal % 10))) : ""; return "Rupees " + Rupees + paise + " Only"; } /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("56721351.61 = " + Currency.convertToIndianCurrency("56721351.61")); } } 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 import java.math.BigDecimal;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap; /** * * @author rajesh kumar sahanee */public class Currency { public static String convertToIndianCurrency(String num) { BigDecimal bd = new BigDecimal(num); long number = bd.longValue(); long no = bd.longValue(); int decimal = (int) (bd.remainder(BigDecimal.ONE).doubleValue() * 100); int digits_length = String.valueOf(no).length(); int i = 0; ArrayList<String> str = new ArrayList<>(); HashMap<Integer, String> words = new HashMap<>(); words.put(0, ""); words.put(1, "One"); words.put(2, "Two"); words.put(3, "Three"); words.put(4, "Four"); words.put(5, "Five"); words.put(6, "Six"); words.put(7, "Seven"); words.put(8, "Eight"); words.put(9, "Nine"); words.put(10, "Ten"); words.put(11, "Eleven"); words.put(12, "Twelve"); words.put(13, "Thirteen"); words.put(14, "Fourteen"); words.put(15, "Fifteen"); words.put(16, "Sixteen"); words.put(17, "Seventeen"); words.put(18, "Eighteen"); words.put(19, "Nineteen"); words.put(20, "Twenty"); words.put(30, "Thirty"); words.put(40, "Forty"); words.put(50, "Fifty"); words.put(60, "Sixty"); words.put(70, "Seventy"); words.put(80, "Eighty"); words.put(90, "Ninety"); String digits[] = {"", "Hundred", "Thousand", "Lakh", "Crore"}; while (i < digits_length) { int divider = (i == 2) ? 10 : 100; number = no % divider; no = no / divider; i += divider == 10 ? 1 : 2; if (number > 0) { int counter = str.size(); String plural = (counter > 0 && number > 9) ? "s" : ""; String tmp = (number < 21) ? words.get(Integer.valueOf((int) number)) + " " + digits[counter] + plural : words.get(Integer.valueOf((int) Math.floor(number / 10) * 10)) + " " + words.get(Integer.valueOf((int) (number % 10))) + " " + digits[counter] + plural; str.add(tmp); } else { str.add(""); } } Collections.reverse(str); String Rupees = String.join(" ", str).trim(); String paise = (decimal) > 0 ? " And Paise " + words.get(Integer.valueOf((int) (decimal - decimal % 10))) + " " + words.get(Integer.valueOf((int) (decimal % 10))) : ""; return "Rupees " + Rupees + paise + " Only"; } /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("56721351.61 = " + Currency.convertToIndianCurrency("56721351.61")); } } Output 56721351.61 = Rupees Five Crore Sixty Seven Lakhs Twenty One Thousands Three Hundred Fifty One And Paise Sixty One Only Thanks Please share if you like it