|
| 1 | +import java.math.BigDecimal; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by mortezasun on 3/29/2017. |
| 5 | + */ |
| 6 | + |
| 7 | +public class PersianNumberToWord { |
| 8 | + private static String[] yekan = new String[]{" یک ", " دو ", " سه ", " چهار ", " پنج ", " شش ", " هفت ", " هشت ", " نه "}; |
| 9 | + private static String[] dahgan =new String[]{" بیست ", " سی ", " چهل ", " پنجاه ", " شصت ", " هفتاد ", " هشتاد ", " نود "}; |
| 10 | + private static String[] sadgan =new String[] {" یکصد ", " دویست ", " سیصد ", " چهارصد ", " پانصد ", " ششصد ", " هفتصد ", " هشتصد ", " نهصد "}; |
| 11 | + private static String[] dah = new String[]{" ده ", " یازده ", " دوازده ", " سیزده ", " چهارده ", " پانزده ", " شانزده ", " هفده ", " هیجده ", " نوزده "}; |
| 12 | + |
| 13 | + public static String onWork(BigDecimal num, String Unit) { |
| 14 | + return onDo(num,0) + " "+Unit; |
| 15 | + } |
| 16 | + private static String onDo(BigDecimal num,int level){ |
| 17 | + if (num == null) { |
| 18 | + return ""; |
| 19 | + } |
| 20 | + // convert negative number to positive and get wordify value |
| 21 | + if (num.compareTo(new BigDecimal(0))<0) { |
| 22 | + num = num.negate(); |
| 23 | + return "منفی " + onDo(num,level); |
| 24 | + } |
| 25 | + if (num.compareTo(new BigDecimal(0))==0) { |
| 26 | + if (level == 0) { |
| 27 | + return "صفر"; |
| 28 | + } else { |
| 29 | + return ""; |
| 30 | + } |
| 31 | + |
| 32 | + } |
| 33 | + String result =""; |
| 34 | + if (level > 0) { |
| 35 | + result += " و "; |
| 36 | + level -= 1; |
| 37 | + } |
| 38 | + |
| 39 | + if (num.compareTo(new BigDecimal(10))<0) { |
| 40 | + result += yekan[num.add(new BigDecimal(1).negate()).intValue()]; |
| 41 | + } else if (num.compareTo(new BigDecimal(20))<0) { |
| 42 | + result += dah[num.add(new BigDecimal(10).negate()).intValue()]; |
| 43 | + } else if (num.compareTo(new BigDecimal(100))<0) { |
| 44 | + result += dahgan[num.divide(new BigDecimal(10)).add(new BigDecimal(2).negate()).intValue()] + onDo(num.remainder(new BigDecimal(10)), level + 1); |
| 45 | + } else if (num.compareTo(new BigDecimal(1000))<0) { |
| 46 | + result += sadgan[num.divide(new BigDecimal(100)).add(new BigDecimal(1).negate()).intValue()] + onDo(num.remainder(new BigDecimal(100)), level + 1); |
| 47 | + } else if (num.compareTo(new BigDecimal(1000000))<0) { |
| 48 | + result += onDo(num.divide(new BigDecimal(1000)), level) + " هزار " + onDo(num.remainder(new BigDecimal(1000)), level + 1); |
| 49 | + } else if (num.compareTo(new BigDecimal(1000000000))<0) { |
| 50 | + result += onDo(num.divide(new BigDecimal(1000000)), level) + " میلیون " + onDo(num.remainder(new BigDecimal(1000000)), level + 1); |
| 51 | + } else if (num.compareTo(new BigDecimal(Long.valueOf("1000000000000")))<0) { |
| 52 | + result += onDo(num.divide(new BigDecimal(Long.parseLong("1000000000"))), level) + " میلیارد " + onDo(num.remainder(new BigDecimal(Long.parseLong("1000000000"))), level + 1); |
| 53 | + } else if (num.compareTo(new BigDecimal(Long.valueOf("1000000000000000")))<0) { |
| 54 | + result += onDo(num.divide(new BigDecimal(Long.parseLong("1000000000000"))), level) + " تریلیارد " + onDo(num.remainder(new BigDecimal(Long.parseLong("1000000000000"))), level + 1); |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | +} |
0 commit comments