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

118 Views Asked by At

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;
        }
    }
2

There are 2 best solutions below

0
Aimar On BEST ANSWER

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");
        }
    }
}
0
softwareguy On
public static String convertToShortForm(long number) {
    if (number < 10000) {
        return Long.toString(number);
    } else if (number < 100000) {
        return String.format("%.1fK", number / 1000.0);
    } else if (number < 1000000) {
        return String.format("%.1fLakh", number / 100000.0);
    } else if (number < 10000000) {
        return String.format("%.1fLakh", number / 1000000.0);
    } else if (number < 100000000) {
        return String.format("%.1fCr", number / 10000000.0);
    } else {
        return String.format("%.1fCr", number / 10000000.0);
    }
}

The above method should work for simple cases. We are just dividing by the number we need and formatting the string for that.