Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Main {
    public static String IndianFormat(BigDecimal n) {
        DecimalFormat formatter = new DecimalFormat("#,###.00");
        //we never reach double digit grouping so return
        if (n.doubleValue() < 100000) {
            return formatter.format(n.setScale(2, 1).doubleValue());
        }
        StringBuffer returnValue = new StringBuffer();
        //Spliting integer part and decimal part
        String value = n.setScale(2, 1).toString();
        String intpart = value.substring(0, value.indexOf("."));
        String decimalpart = value.substring(value.indexOf("."), value.length());
        //switch to double digit grouping
        formatter.applyPattern("#,##");
        returnValue.append(formatter.format(new BigDecimal(intpart).doubleValue() / 1000)).append(",");
        //appending last 3 digits and decimal part
        returnValue.append(intpart.substring(intpart.length() - 3, intpart.length())).append(decimalpart);
        //returning complete string
        if (returnValue.toString().equals(".00")) {
            return "0.00";
        }

        return returnValue.toString();

    }
}