Bangladeshi Currency Format in Java and Javascript

In international standard, 100K will be written like 100,000. But in the BD currency system, it will be written like 1,00,00. That means we have a comma after thousand, lac, crore. So today we will develop a coding snippet, where we will input a number in string format and it will do Bangladeshi currency format in java and javascript

import java.util.Scanner;

public class BDCurrencyFormat {
    public static void main(String arg[]){
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        System.out.println(formatedPrice(str));
    }
    private static String formatedPrice(String str) {

        int counter=0;
        boolean startCount=false;
        for (int i=0;i<str.length();i++){
            if (str.charAt(i)=='.' || startCount){
                startCount=true;
                counter++;
            }
        }
        int except=counter+3;
        if (str.length() > except) {
            StringBuilder ts = new StringBuilder();
            int i = 0;
            int preFormatterLength = str.length() - except;
            if (preFormatterLength % 2 == 1) {
                i = 1;
                ts.append(str.charAt(0)).append(',');
            }
            ts.append(operateFormat(i,preFormatterLength,str));
            for (int j = str.length() - except; j < str.length(); j++) {
                ts.append(str.charAt(j));
            }
            return ts.toString();
        } else {
            return str;
        }
    }
    private static String operateFormat(int i, int preFormatterLength, String str){
        StringBuilder formattedString=new StringBuilder();
        boolean comma=true;
        for (; i < preFormatterLength; i++) {
            if (comma) {
                comma = false;
                formattedString.append(str.charAt(i));
            } else {
                comma = true;
                formattedString.append(str.charAt(i)).append(',');
            }
        }
        return formattedString.toString();
    }
}

In Typescript, We can implement like below.

MoneyFormat(str: string) {
    let counter = 0;
    let shouldCount = false;
    let i = 0;
    for (; i < str.length; i++) {
      if (str[i] === '.' || shouldCount) {
        counter = counter + 1;
        shouldCount = true;
      }
    }
    const except = counter + 3;
    if (str.length > except) {
      let formattedMoney = '';
      i = 0;
      const preFormatterLength = str.length - except;
      if (preFormatterLength % 2 === 1) {
        i = 1;
        formattedMoney = formattedMoney + str[0] + ',';
      }
      let comma = true;
      for (; i < preFormatterLength; i++) {
        if (comma) {
          comma = false;
          formattedMoney = formattedMoney + str[i];
        } else {
          comma = true;
          formattedMoney = formattedMoney + str[i] + ',';
        }
      }
      for (let j = str.length - except; j < str.length; j++) {
        formattedMoney += str[j];
      }
      return formattedMoney;
    } else {
      return str;
    }
  }
222 Views

About Author

Nasir

Welcome to CodeKit.xyz. I am working in an automation company as a software engineer. From the deep thirst of learning, I have developed this platform.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>