Search code examples
javaandroidnumber-formatting

how to number value in convert in indian currency format. like (120000 to 1.2Lakh & 50000000 to 5Cr


Hello guys I want to convert my number value in short form but in indian value short form

like

6500 to 6.5K

50000 to 50K

99900 to 99.9K

100000 to 1Lakh

150000 to 1.5Lakh

1000000 to 10Lakh

1450000 to 14.5Lakh

50000000 to 5Cr

55000000 to 5.5Cr

I give my solution here and it's working properly

private static String indianCurrencyShortForm(double amount) {

        double finalVal;
        String postfix;
        if (amount < 1000) {
            return String.valueOf(amount);
        } else if (amount < 100000) {
            finalVal=amount / 1000;
            postfix="K";
        } else if (amount < 10000000) {
            finalVal=amount / 100000;
            postfix="L";
        } else {
            finalVal=amount / 10000000;
            postfix="Cr";
        }
        boolean isRound = (finalVal * 10) % 10 == 0;
        if (isRound){
            return((int) finalVal)+postfix;
        }else {
            /*Reason for split string because while input is 99999 it converts into 100.0K
            * return String.format("%.1f", finalVal)+postfix;
            * */
            String[] split=String.valueOf(finalVal).split("\\.");
            char fractionVal=split[1].charAt(0);
            return (split[0]+(fractionVal!='0'?("."+fractionVal):""))+postfix;
        }
    }

Solution

  • Your question is not quite clear, but I think you are looking for the following code:

    import java.text.DecimalFormat;
    
    public class Convert {
    
        public static String FilterV(long num) {
            if (amount < 1000) {
              return String.valueOf(amount);
            } else if (amount < 100000) {
              finalVal=amount / 1000;
              postfix="K";
            } else if (amount < 10000000) {
              finalVal=amount / 100000;
              postfix="L";
            } else {
              finalVal=amount / 10000000;
              postfix="Cr";
            }
            boolean isRound = (finalVal * 10) % 10 == 0;
            if (isRound){
               return((int) finalVal)+postfix;
            }else {
            /*Reason for split string because while input is 99999 it convert into 100.0K
            * return String.format("%.1f", finalVal)+postfix;
            * */
            String[] split=String.valueOf(finalVal).split("\\.");
            char fractionVal=split[1].charAt(0);
            return (split[0]+(fractionVal!='0'?("."+fractionVal):""))+postfix;
          }
        }
    
        public static void main(String[] args) {
            if (args.length != 1) {
                System.out.println("Usage: java Convert <number>");
                return;
            }
            try {
                long number = Long.parseLong(args[0]);
                System.out.println(number + " => " + FilterV(number));
            } catch (NumberFormatException e) {
                System.out.println("Error Exception: Input is not valid");
            }
        }
    }