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;
import java.text.DecimalFormatSymbols;

public class Main {
    public static String safeDoubleToCurrency(Double val) {
        BigDecimal value = BigDecimal.valueOf(val);
        DecimalFormat kursIndonesia = (DecimalFormat) DecimalFormat.getCurrencyInstance();
        DecimalFormatSymbols formatRp = new DecimalFormatSymbols();

        formatRp.setCurrencySymbol("");
        formatRp.setMonetaryDecimalSeparator('.');
        formatRp.setGroupingSeparator(',');

        kursIndonesia.setDecimalFormatSymbols(formatRp);
        kursIndonesia.setParseBigDecimal(true);
        if (val < 1)
            kursIndonesia.setMaximumFractionDigits(8);
        else if (val < 10)
            kursIndonesia.setMaximumFractionDigits(6);
        else if (val < 100)
            kursIndonesia.setMaximumFractionDigits(4);

        return kursIndonesia.format(value);
    }
}