Computer Science, asked by vikas334, 1 year ago

write a program in Java to convert the amount to rupees and paise and print it with variable description table

Answers

Answered by Ankit079
0


Currency.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

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"));

    }

 

}

Similar questions